Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1927    Accepted Submission(s): 965

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
 
Source
 
 
题意:  给你一些顶点的最短距离,要你求出原图最少含有多少边。
我们知道,最短路所构成的图已经是最少的边。所以只需要用n个点构成的有向边的和n*(n-1)减去那些合成的边。就是最少的边。
采用floy算法,松弛度来勾结一个最短图...
代码:
 #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的深入理解)的更多相关文章

  1. HDU 4034 Graph(Floyd变形——逆向判断)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4034 Problem Description Everyone knows how to calcu ...

  2. HDU 4034 Graph Floyd最短路

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...

  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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...

  6. 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-> ...

  7. hdu 4034 Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题目分类:图论 题意:n个顶点,然后给出从i到j的最短路径长度,求至少需要哪些边 第二组样例 第 ...

  8. Codeforce 295B Greg and Graph(Floyd的深入理解)

    题目链接:http://codeforces.com/problemset/problem/295/B 题目大意:给出n个点的完全有权有向图,每次删去一个点,求删掉该点之前整张图各个点的最短路之和(包 ...

  9. [la P5031&hdu P3726] Graph and Queries

    [la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. 表头表侧边固定,方法二,丫的,复制td

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. Examples For When-Validate-Item trigger In Oracle Forms

    The following example finds the commission plan in the COMMPLAN table, based on the current value of ...

  3. ARM工作模式

    ARM工作模式 学习ARM的最好的资料是ARM公司发布的资料:ARM Architecture Reference Manual.pdf 找到章节:Programmers’ Model 由文档可知:A ...

  4. MusigCV安装

    首先,将下载的安装文件zip包,http://www.mathworks.com/products/compiler/mcr/ MCR2013a 然后依次执行下面的命令: 进入目录:cd /tmp 解 ...

  5. Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...

  6. tilemap坐标转换

    像素点跟tile的索引之间的转换//从cocos2d-x坐标转换为Tilemap坐标CCPoint GameMap::tileCoordForPosition(CCPoint position){ i ...

  7. JavaWeb项目的classpath说明

    概念解释: classpath : 即项目中WEB-INF下面的classes目录; 应用: [01] src路径下的文件在编译后会放到WEB-INF/classes路径下.默认的classpath是 ...

  8. mongoDB 修改器()

    -----------------------------------2016-5-26 15:56:57-- source:[1],MongoDB更新操作符

  9. nodejs学习笔记<五>npm使用

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 以下是几种常见使用场景: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  10. poj2420A Star not a Tree?(模拟退火)

    链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...