题目链接:

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. C# 调用动态链接库,给游览器写入Cookie

    样例代码: class Program { /// <summary> /// 写 /// </summary> /// <param name="lpszUr ...

  2. linux apache虚拟主机配置(基于ip,端口,域名)

    配置环境: linux版本:Centos6.4 httpd版本: [root@centos64Study init.d]# pwd/etc/init.d[root@centos64Study init ...

  3. CSS3 黑白图片

    每当有自然灾害的时候,很多网站都是灰白的,想知道是怎么实现的嘛? 1.IE私有滤镜的方式 自IE4开始,IE引入了私有滤镜,可以实现透明度.模糊.阴影.发光等效果,当然也可以实现灰度图像效果.代码如下 ...

  4. centos6环境下使用yum安装Ambari

    前言: Ambari是apache下面的开源项目,主要通过web UI方式对Hadoop集群进行统一创建和管理,以节省Hadoop集群的运维成本.本文通过安装过程中的截图简要介绍一下相关步骤供需要的朋 ...

  5. vue2.0 关于Vue实例的生命周期

    什么是生命周期 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁的过 ...

  6. 不会PS如何自制简单线条、任意填充色的小图标

    最近在做H5的开发中,需要用到一些简单的小图标,百度出来的图片,总是或多或少差了一些颜色.于是准备自己制作图片,PS是不会的,学习以及软件安装太费时,于是就准备用常见的软件来试着做一做. 在尝试了 w ...

  7. 实战开发-》融云tp3.2.3

    1.先去下载sdk 2.我放在的位置如下: 3.之前试了试,怎么引入都不成功,所以我加入了命名空间 还有,我把methods下的所有类都加了命名空间,心里安慰吧. 4.在公共函数写函数,例如 填写自己 ...

  8. python如何玩“跳一跳”!(windows安桌版本请进!)

    最近"跳一跳",很火爆,有木有? 看了一下网上的教程,动作搭建了一下环境,就可以用脚本自动跑起来啦!!! 下面说一下android手机的实现过程: 首先,是python环境的搭建 ...

  9. python matplotlib 绘图基础

    在利用Python做数据分析时,探索数据以及结果展现上图表的应用是不可或缺的. 在Python中通常情况下都是用matplotlib模块进行图表制作. 先理下,matplotlib的结构原理: mat ...

  10. Mac 下 android/iOS https抓包

    一.Charles简介 Charles,是用Java开发的,所以跨平台,不仅可以在Mac上使用,Linux以及Window下都是可以使用的,当然需要安装JDK,才能运行,但目前是收费的. 二.下载 官 ...