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: ...
随机推荐
- mac 配置jdk maven
1.从oracle下载jdk 链接:http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后安装jdk 2.下载Mav ...
- sar 找出系统瓶颈的利器
sar 找出系统瓶颈的利器sar是System Activity Reporter(系统活动情况报告)的缩写.sar工具将对系统当前的状态进行取样,然后通过计算数据和比例来表达系统的当前运行状态.它的 ...
- iOS - UIActivityViewController
前言 NS_CLASS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED @interface UIActivityViewController : UIViewControl ...
- Nginx模块学习之————accesskey权限模块使用(简单的m3u8防盗链)
配置文件:http://www.cnblogs.com/tinywan/p/5983694.html 通过加密后的文件: 正确地址:curl -i http://访问的IP地址(这里是直播节点IP地址 ...
- js自定义弹窗
<一>confirm弹窗 页面操作中常见需要确认操作. 例如:删除某条消息前需要确认是否删除. 页面中弹窗确认操作用到confirm消息对话框. JS代码 function del(){ ...
- 关于ASP.NET的web.config的小笔记
在ASP和MVC开发中,有一些参数是需要活动更改的,最常见的就是数据库的链接字符串<connectionStrings>节点下配置的.在今天接触的项目中,我又接触到了自定义配置参数,就是可 ...
- js打印出对象的方法
var description = ""; for (var i in order) { var property = order[i]; description += i + & ...
- JAVA必背面试题和项目面试通关要点
一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选择重复项,然后去掉重复项:) 数据库里 ...
- Linux命令之乐--awk
1.脚本参数传值 #/bin/bash awk '"} {if(($1==a)) print $2;}' /etc/hosts 执行结果:
- white-space: nowrap
CSS:需要加上宽度(width:100px).超出隐藏(overflow:hidden;).强制在同一行显示(white-space: nowrap;).省略号(text-overflow:elli ...