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: ...
随机推荐
- 《安全智库》:48H急速夺旗大战通关writeup(通关策略)
作者:ByStudent 题目名字 题目分值 地址 MallBuilder2 350 mall.anquanbao.com.cn MallBuilder1 200 mall.anquanbao.c ...
- 用sql的select语句从数据库中获取数据
基本的select语句 select语句中的算数表达式和NULL值 列的别名 使用连接符操作,literal character strings,alternative quote operator, ...
- CUBRID学习笔记 6 修改用户密码
修改密码 可以在web管理中修改. 还有另外两种体位 1 语句 ALTER USER user_name PASSWORD 'any_password_here_in_single_quotes'; ...
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- python_way ,day9 socket 和soctet多线程的使用
1.socket的基础 tcp/ip tcp ip 协议是主机接入互联网以及接入互联网互联两台主机通信互联标准 物理层:传输二进制 链路层:对二进制数进行分组 ,唯一标示一台主机 ...
- STORM_0002_在做好的zookeeper集群上搭建storm的开发环境
参考文献http://www.cnblogs.com/panfeng412/archive/2012/11/30/how-to-install-and-deploy-storm-cluster.htm ...
- [原]五分钟搭建gitserver
本来在忙一些事情,结果刚才突然收到一个临时的事情,号称很着急. 问了一下,原来是需要在本地搭建一个git库,但其实之前我是有做过gitserver的,不过是在阿里云(部分分布在青云)上,而且目前在使用 ...
- XAF Excel数据导入模块使用说明与源码
我实现了XAF项目中Excel数据的导入,使用Devexpress 新出的spreadsheet控件,可能也不新了吧:D 好,先看一下效果图:下图是Web版本的. 下面是win版: 功能说明: 支持从 ...
- C#中Monitor和Lock以及区别
Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...
- grep 简单使用
grep "关键字" file文件名 | tail -100|grep "关键字" --col grep的功能 grep从一个或多个文本文件中查 ...