hdu 4034 Graph (floyd的深入理解)
Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1927 Accepted Submission(s): 965
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?
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.
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.
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
#include<cstdio>
#include<cstring>
#define maxn 110
int ds[maxn][maxn];
bool vis[maxn][maxn];
int mat[maxn][maxn];
void floyd(int n)
{
for(int k=;k<n;k++)
{
for(int i=;i<n;i++)
{
if(i==k) continue;
for(int j=;j<n;j++)
{
if(k==j)continue;
if(ds[i][j]>=ds[i][k]+ds[k][j])
{
vis[i][j]=;
ds[i][j]=ds[i][k]+ds[k][j];
}
}
}
}
}
int main()
{
int cas,n;
scanf("%d",&cas);
for(int tt=;tt<=cas;tt++)
{
scanf("%d",&n);
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
scanf("%d",mat[i]+j);
ds[i][j]=mat[i][j];
}
memset(vis,,sizeof(vis));
floyd(n);
int res=;
bool tag=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(vis[i][j]&&ds[i][j]==mat[i][j])
res++;
else if(ds[i][j]<mat[i][j])
{
tag=;
break;
}
}
if(tag)break;
}
printf("Case %d: ",tt);
if(tag)
printf("impossible\n");
else printf("%d\n",n*(n-)-res); }
return ;
}
hdu 4034 Graph (floyd的深入理解)的更多相关文章
- HDU 4034 Graph(Floyd变形——逆向判断)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4034 Problem Description Everyone knows how to calcu ...
- HDU 4034 Graph Floyd最短路
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...
- HDU 4034 Graph(floyd,最短路,简单)
题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...
- HDU 4034 Graph:反向floyd
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...
- 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-> ...
- hdu 4034 Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...
- Codeforce 295B Greg and Graph(Floyd的深入理解)
题目链接:http://codeforces.com/problemset/problem/295/B 题目大意:给出n个点的完全有权有向图,每次删去一个点,求删掉该点之前整张图各个点的最短路之和(包 ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
随机推荐
- <转>离婚前夜悟出的三件事
文/铁眼(简书作者)原文链接:http://www.jianshu.com/p/832be4f659a0?utm_campaign=hugo&utm_medium=reader_share&a ...
- 关于两个php.ini的说明
关于两个php.ini的说明 Apache 和 php下各有一个php,ini文件 D:\wamp\bin\apache\apache2.4.9\bin\php.ini用于web访问时的配置文 ...
- 各种编码中汉字所占字节数;中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
vim settings set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936,latin1set termencoding=utf-8se ...
- 多线程技术在iOS开发中的使用
进程和线程 要使用多线程,首先需要理解进程和线程这2个概念.这里我简单的说一下. 所谓进程对应的是一个应用程序,负责开辟内存空间供应用程序使用,但是进程不能执行任务(指令).一个进程至少包含一条线程, ...
- 用Hbase存储Log4j日志数据:HbaseAppender
业务需求: 需求很简单,就是把多个系统的日志数据统一存储到Hbase数据库中,方便统一查看和监控. 解决思路: 写针对Hbase存储的Log4j Appender,有一个简单的日志储存策略,把Log4 ...
- 3.27考试总结(hnoi难度)
1 duel1.1 DescriptionK·⁄¶("“uƒ¢y%¥§H5|+ (?1’m" ’m·ø?1§z<k§⁄k<⁄§lm?0,1,2...§zg ‹‰ª«mS ...
- poj3714Raid(平面最近点对)
链接 模板 稍加一点标记 模板 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- java里有没有专门判断List里有重复的数据
public static void main(String[] args) { List<String> list = new ArrayList<Stri ...
- css+js实现兼容性select的样式
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- go语言中间的循环
在Go语言中只有很少的几个控制结构,它没有while或者do-while循环. 但是它有for.switch.if.而且switch接受像for那样可选的初始化语句.下面来认识一下他们 一.if语句 ...