Description

Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexes, can you find the original graph?
 

Input

The first line is the test case number T (T ≤ 100).
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.
 

Output

For each case, you should output “Case k: ” first, where k indicates
the case number and counts from one. Then one integer, the minimum
possible edge number in original graph. Output “impossible” if such
graph doesn't exist.
 

Sample Input

3
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 Sample Output
Case 1: 6
Case 2: 4
Case 3: impossible 题目大意:给一张已经用floyd求好最短路的图,问最少可由多少条边得到。
题目解析:对于边e[i][j],如果存在e[i][j]=e[i][k]+e[k][j],则边i->j没有必要存在;如果存在e[i][j]>e[i][k]+e[k][j],则图有误,impossible。将最外层循环放到最内层即可。 代码如下:
 # include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
int mp[][],n;
int ok()
{
int i,j,k;
int cnt=,vnt=;
for(i=;i<=n;++i){ ///枚举每
for(j=;j<=n;++j){ ///一条边
if(mp[i][j]!=)
++vnt;
if(i==j)
continue;
for(k=;k<=n;++k){ ///枚举中间节点
if(i==k||j==k)
continue;
if(mp[i][j]>mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
return -;
}else if(mp[i][j]==mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
++cnt;
break;
}
}
}
}
return vnt-cnt;
}
int main()
{
int T,i,j,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;++i)
for(j=;j<=n;++j)
scanf("%d",&mp[i][j]);
printf("Case %d: ",++cas);
int ans=ok();
if(ans!=-)
printf("%d\n",ans);
else
printf("impossible\n");
}
return ;
}

Graph (floyd)的更多相关文章

  1. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  2. Graph(Floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  3. WUSTOJ 1326: Graph(Java)费马数

    题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...

  4. (floyd)佛洛伊德算法

    Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...

  5. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  6. 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 ...

  7. Stockbroker Grapevine(floyd)

    http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...

  8. 2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)

    [传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们 ...

  9. Floyed(floyd)算法详解

    是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...

随机推荐

  1. git2

    1,开源的代码管理工具 2,分布式管理工具(更安全,可以脱网操作) 3,git的分支管理更加便捷. 4,代码的传输更新速度更快 利用git可以进入多人配合代码开发.有备份.协同 sudo apt-ge ...

  2. React 回忆录(三)使用 React 渲染界面

    Hi 各位,欢迎来到 React 回忆录!

  3. 如何使用python来对二维数组进行排序

    1.复合排序 直接用numpy的lexsort就可以 import numpy as np data = np.array([[1,2,3,4,5], [1,2,3,6,7], [2,3,4,5,7] ...

  4. 详解C中的volatile关键字【转】

    本文转载自:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有 ...

  5. Python 代码片段收藏

    list 列表相关 list 中最小值.最大值 import operator values = [1, 2, 3, 4, 5] min_index, min_value = min(enumerat ...

  6. 全球变暖|2018年蓝桥杯B组题解析第九题-fishers

    标题:全球变暖 你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: ....... .##.... .##.... ....##. .. ...

  7. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  8. 批量启动application pool

    在powershell中执行 Get-ChildItem IIS:\AppPools | where {$_.state -eq "Stopped"} | Start-WebApp ...

  9. java代码实现highchart与数据库数据结合完整案例分析(一)---饼状图

    作者原创:转载请注明出处 在做项目的过程中,经常会用到统计数据,同时会用到highchart或echart进行数据展示,highchart是外国开发的数据统计图插件, echart是我们国家开发的数据 ...

  10. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...