题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4034

Problem 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算法跑出来的最短路径的邻接矩阵
判断是否有这样的图存在,存在输出图中至少需要几条边,不存在输出impossible
解题思路:
首先分为存在和不存在两种情况
不存在意味该图中存在至少一条边的最短路径是错误的,例如图中存1--->2为3,2--->3为4,而图中存储的是2--->3为8,很明显可以通过2这个点将1--->3这条边松弛为7,那么这条最短路径是错误的,近而判断不存在这样的图;
再说存在这样的图的情况,如果存在这样的图,根据题中所说为单向道路,n个顶点最多有n*(n-1)条边,如果某一条边可以消去,那么必定可以找到与直接路径相等的间接路径,例如图中存1--->2为3,2--->3为4,而图中存储的是1--->3为7,很明显可以将1--->3这条边消去。
另外,每次判断,需要用book数组来记录那一条边已经消去过了,否则可能重复消去。
代码实现:
 #include<stdio.h>
#include<string.h>
int n,map[][],book[][];
int floyd();
int main()
{
int T,t=,i,j,flag;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]);
flag=floyd();
if(flag)
printf("Case %d: %d\n",++t,flag);
else
printf("Case %d: impossible\n",++t);
}
return ;
}
int floyd()
{
int i,j,k,ans;
memset(book,,sizeof(book));
ans=n*(n-);
for(k=;k<=n;k++)
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i != j&&i != k&&j != k)
{
if(!book[i][j] && map[i][j] == map[i][k]+map[k][j])
{
ans--;
book[i][j]=;
}
else if(map[i][j] > map[i][k]+map[k][j])
return ;
}
return ans;
}
 
 

HDU 4034 Graph(Floyd变形——逆向判断)的更多相关文章

  1. HDU 4034 Graph Floyd最短路

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...

  2. hdu 4034 Graph (floyd的深入理解)

    Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submi ...

  3. HDU 4034 Graph(floyd,最短路,简单)

    题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> ...

  4. hdu 4034 Graph floyd

    题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...

  5. hdu 4034 Graph(逆向floyd)

    floyd的松弛部分是 g[i][j] = min(g[i][j], g[i][k] + g[k][j]);也就是说,g[i][j] <= g[i][k] + g[k][j] (存在i-> ...

  6. HDU 4034 Graph:反向floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...

  7. hdu 1596(Floyd 变形)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/ ...

  8. hdu 1217 (Floyd变形)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)   ...

  9. hdu 4034 Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...

随机推荐

  1. Docker(九):Docker容器卷插件

    1.Convoy 1.1 安装 [root@MediaServer tmp]# tar xvf convoy.tar.gz convoy/ convoy/convoy-pdata_tools conv ...

  2. Spring Dynamic DataSource Routing

    Use AbstractRoutingDataSource to dynamicly switch datasources, see http://spring.io/blog/2007/01/23/ ...

  3. 关于php中,记录日志中,将数组转为json信息记录日志时遇到的问题总结

    1 中文编码化,无法看到具体的中文,如:你好  =>  \u4F60\u597D 解决方案:可以使用 json_encode($arr,JSON_UNESCAPED_UNICODE) 转义中文[ ...

  4. 怎么制作html5网站页面让它适应电脑和手机的尺寸

    https://zhidao.baidu.com/question/918130826792192539.html 用以下代码开头:<!DOCTYPE HTML><html>& ...

  5. python中星号的意义(**字典,*列表或元组)

    传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊的语法:*.** . 调用函数时使用* ,** test(*args)中 * 的作用:其 ...

  6. Qt实现QQ界面

    1.Qt实现QQ界面是通过QToolBox类来实现的,基本结构是:QToolBox里面装QGroupBox,然后QGroupBox里面装QToolButton,设置好相关属性即可 2.定义类继承QTo ...

  7. MicroPython-GPRS教程之TPYBoardv702GPRS功能测试

    一.什么是TPYBoardV702 TPYBoardV702是目前市面上唯一支持通信通信功能的MicroPython开发板:支持Python3.0及以上版本直接运行.支持GPS+北斗双模通信.GPRS ...

  8. DNN论文分享 - Item2vec: Neural Item Embedding for Collaborative Filtering

    前置点评: 这篇文章比较朴素,创新性不高,基本是参照了google的word2vec方法,应用到推荐场景的i2i相似度计算中,但实际效果看还有有提升的.主要做法是把item视为word,用户的行为序列 ...

  9. Visual Studio 中添加SQLite数据源

    相关下载:https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 在Visual Studio中要支持访问SQLi ...

  10. [转载自阿里丁奇]各版本MySQL并行复制的实现及优缺点

    MySQL并行复制已经是老生常谈,笔者从2010年开始就着手处理线上这个问题,刚开始两三年也乐此不疲分享,现在再提这个话题本来是难免"炒冷饭"嫌疑.    最近触发再谈这个话题,是 ...