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. [转载]INNER JOIN连接两个表、三个表、五个表的SQL语句

    SQL INNER JOIN关键字表示在表中存在至少一个匹配时,INNER JOIN 关键字返回行. 1.连接两个数据表的用法: FROM Member INNER JOIN MemberSort O ...

  2. Django框架----基础

    一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...

  3. mysql explicit_defaults_for_timestamp 变量的作用

    mysql 中有这样的一个默认行为,如果一行数据中某些列被更新了,如果这一行中有timestamp类型的列,那么么这个timestamp列的数据 也会被自动更新到 更新操作所发生的那个时间点:这个操作 ...

  4. Linux 虚拟机安装vmware tools

    Linux Vmware tools安装步骤 1 在 vSphere Client 清单中,右键单击虚拟机,然后选择电源 > 开启.   2 单击控制台选项卡以确定客户机操作系统启动成功,并在需 ...

  5. PHP-Iterator迭代器(遍历)接口详讲

    echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> "; class ...

  6. 求LCA练习+部分算法复习 2017.1.22

    第一题就LCA即可.不过推荐用Tarjan(最快,常数很小).然后Tarjan的时候顺便就出一个dist[i],表示i节点到根节点的距离.求出了LCA,那么两点间的距离就为dist[u] + dist ...

  7. 理解 python 中__name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  8. ZooKeeper 增加Observer部署模式提高性能

    Observer:在不伤害写性能的情况下扩展ZooKeeper. 虽然通过Client直接连接到ZooKeeper集群的性能已经很好了,可是这样的架构假设要承受超大规模的Client,就必须添加Zoo ...

  9. 试着用React写项目-利用react-router解决跳转路由等问题(二)

    转载请注明出处:王亟亟的大牛之路 这一篇还是继续写react router相关的内容,废话之前先安利:https://github.com/ddwhan0123/Useful-Open-Source- ...

  10. BZOJ4401: 块的计数 思维题

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...