Graph(Floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=4034
Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2058 Accepted Submission(s): 1030
First line of each case is an integer N (1 ≤ N ≤ 100), the number of vertexes.
Following N lines each contains N integers. All these integers are less than 1000000.
The jth integer of ith line is the shortest path from vertex i to j.
The ith element of ith line is always 0. Other elements are all positive.
3
0 1 1
1 0 1
1 1 0
3
0 1 3
4 0 2
7 3 0
3
0 1 4
1 0 2
4 2 0
Case 2: 4
Case 3: impossible
题意:给出由已知点求出的每个点间的最短路,问你原先的图中最少有几个点
题解:对已经给出的最短路再求一遍最短路用Floyd ,如果在求得过程中发现有dist[i][j]>dist[i][k]+dist[k][j]的情况就说明所给的不是最短的路图,及impossible
而在求解的过程中,当dist[i][j]==dist[i][k]+dist[k][j]的时候说明从i 到j 的长度,可以通过k点到达,故可以将直接相连的i,j去掉,及标记dist[i][j] = INF;
注意两点: 1,可以先将impossible的情况单独先算出来,以防后面对dist[i][j] = INF ;
2, 当i==j||j==k||j==k 的时候要continue掉,因为这个为0的点会更新其他所有的点
下面是代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 103
#define INF 0x1fffffff
int mp[N][N];
int dist[N][N];
int main()
{
int i , j , k ;
int n;
int t ;
cin>>t;
int c = ;
while(t--)
{
c++;
scanf("%d",&n);
for( i = ; i < n ;i++)
{
for( j = ; j < n ;j++)
{
scanf("%d",&mp[i][j]);
dist[i][j] = mp[i][j];
}
}
bool flag = true;
for(k = ;flag && k < n ; k++)
{
for(i = ;flag && i < n ; i++)
{
for( j = ; flag&& j < n ;j++)
{
if(dist[i][j]>dist[i][k]+dist[k][j])
flag = false;
}
}
}
int cnt = ;
if(flag)
{
for( k = ; k < n ;k++)
{
for(i = ; i < n ;i++)
{
for(j = ;j < n ;j++)
{
if(i==j||j==k||k==i) continue;
if(dist[i][j]==dist[i][k]+dist[k][j])
{
dist[i][j] = INF;
//printf("%d %d %d\n", k ,i , j);
cnt++;
}
}
}
}
}
if(flag) printf("Case %d: %d\n",c,n*(n-)-cnt);
else printf("Case %d: impossible\n",c); }
return ;
}
Graph(Floyd)的更多相关文章
- [CodeForces - 296D]Greg and Graph(floyd)
Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...
- Graph (floyd)
Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the oppo ...
- WUSTOJ 1326: Graph(Java)费马数
题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...
- (floyd)佛洛伊德算法
Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...
- POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- Stockbroker Grapevine(floyd)
http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...
- 2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)
[传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们 ...
- Floyed(floyd)算法详解
是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...
随机推荐
- 简单记录一下原生ajax
面试老忘记,代码如下 function ajax() { var xmlHttpRequest = null; //定义XMLHttp对象的容器 if(window.XMLHttpRequest) { ...
- Linux根目录详解-转自鸟哥的私房菜
转自:http://myhat.blog.51cto.com/391263/107931/ *根目录(/)的意义与内容: 根目录是整个系统最重要的一个目录,因为不但所有的目录都是由根目录衍生出来的 ...
- Regular expressions in lexing and parsing(翻译)
词法分析和语法分析中的正则表达式 (英文原文来自rob pike 的博客 https://commandcenter.blogspot.jp/2011/08/regular-expressions-i ...
- centos 7 部署 汉化版 gitlab
=============================================== 2017/11/12_第6次修改 ccb_warlock 更 ...
- Linux(CentOS6.5)下编译安装PHP5.6.22时报错”configure: error: ZLIB extension requires gzgets in zlib”的解决方式(确定已经编译安装Zlib,并已经指定Zlib路径)
本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 今天在CentOS6.5下编译安装PHP时,一直报错 confi ...
- python 爬去拉钩测试招聘信息
代码如下: #coding:utf-8 import time import urllib.request from bs4 import BeautifulSoup file=open(r'meit ...
- 微服务架构介绍和RPC框架对比
微服务架构介绍和RPC框架对比 1.微服务架构 1.1 特征 自动化部署,端点智能化,语言和数据的去中心化控制. 1.2架构 一种将一个单一应用程序开发为一组小型服务的方法,每个服务运行在自己的进程中 ...
- 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件
XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...
- java多线程(二)-Runnable和Thread
Java在顺序性语言的基础上提供了多线程的支持.Java的线程机制是抢占式的.这表示调度机制会周期的中断线程,将上下文切换到另一个线程,从而为每个线程都提供时间片.(与抢占式多线程对应的是 协作式多线 ...
- Python day 7(1) 模块
一:模块 1 在Python中,一个.py文件就称之为一个模块(Module) 2 Python的好处,优点: a 提高了代码的可维护性 b 当一个模块编写完毕,就可以被其他地方引用.我们在编写程 ...