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: ...
随机推荐
- 3d中的向量
向量基础知识 向量有且仅有的两个属性:方向长度(注意:向量性情中不包含位置信息) 向量相等就是其两个属性相等 向量处理标记位置:当向量的起始点与坐标原点重合,这样我们就可以用向量的终点坐标来描述一个处 ...
- 蒙特卡洛法计算定积分—Importance Sampling
如上图所示,计算区间[a b]上f(x)的积分即求曲线与X轴围成红色区域的面积.下面使用蒙特卡洛法计算区间[2 3]上的定积分:∫(x2+4*x*sin(x))dx # -*- coding: u ...
- SQL Server小技巧【1】
1.SQL防止修改数据时引起多用户并发,当一条数据被一个用户锁定的时候其他用户将无法修改,除非将其释放. UPDATE TABLENAME WITH(ROWLOCK) SET 字段='Value' W ...
- HNOI2006-鬼谷子的钱袋
鬼谷子的钱袋 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一件宝物引起 ...
- C# 创建一个日志文件
private static string m_fileName = "C:\\ErrorMsg.txt"; public static void CreateErrorLog(s ...
- FlexSlider插件的详细设置参数 http://www.woothemes.com/flexslider/ -----幻灯片插件
$(window).load(function() { $('.flexslider').flexslider({ namespace: 'flex-', //控件的命名空间,会影响样式前缀 anim ...
- 如何写好CSS?(OOCSS\DRY\SMACSS)
我现在面对的CSS基本上就是一个三头六臂的怪物,一点不夸张,因为真的是三头六臂,同一个样式在同一个element上作用了好几遍,而同一个样式又分散在4,5个class上,优先级有很多层.可以看得出这个 ...
- iOS - OC NSKeyedArchiver 数据归档
前言 @interface NSKeyedArchiver : NSCoder @interface NSKeyedUnarchiver : NSCoder 在 OC 语言中,归档是一个过程,即用某种 ...
- Nginx模块学习之————accesskey权限模块使用(简单的m3u8防盗链)
配置文件:http://www.cnblogs.com/tinywan/p/5983694.html 通过加密后的文件: 正确地址:curl -i http://访问的IP地址(这里是直播节点IP地址 ...
- CentOS6.4_常用命令
1. 查看本机是否安装了 xxx软件,以及 xxx软件 的版本信息等: rpm -qa |grep xxx(xxx代表软件名) 2. rpm -ivh 要安装的rpm文件包 3. 不care依赖项的 ...