HDOJ 题目4034 Graph(逆向Floyd)
2014-12-29 23:49
260 查看
Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1992 Accepted Submission(s): 996
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
The 36th ACM/ICPC Asia Regional
Chengdu Site —— Online Contest
Recommend
lcy | We have carefully selected several similar problems for you: 4039 4038 4036 4033 4037
floyd的松弛部分是 g[i][j] = min(g[i][j], g[i][k] + g[k][j]);也就是说,g[i][j] <= g[i][k] + g[k][j] (存在i->j, i->k, k->j的边)。
ac代码
#include<stdio.h> #include<string.h> int map[1010][1010],v[1010][1010]; int main() { int t,c=0,k; scanf("%d",&t); while(t--) { int n,i,j,ans,w=1; scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) scanf("%d",&map[i][j]); } ans=n*(n-1); memset(v,0,sizeof(v)); for(k=0;k<n;k++) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(k==i||j==k) continue; if(map[i][j]>map[i][k]+map[k][j]) { w=0; break; } if(!v[i][j]&&map[i][j]==map[i][k]+map[k][j]) { ans--; v[i][j]=1; } } if(!w) break; } if(!w) break; } if(w) printf("Case %d: %d\n",++c,ans); else printf("Case %d: impossible\n",++c); } }
相关文章推荐
- hdu 4034 Graph(逆向floyd)
- hdu 4034 Graph(逆向floyd)
- HDU 4034 Graph(Floyd变形——逆向判断)
- HDU 4034 Graph(Floyd)
- HDU 4034 Graph(11年成都 Floyd运用)
- hdu 4034 Graph解题报告-Floyd思想
- HDU 4034 Graph(Floyd变形)
- HDU 4034 Graph【最短路之floyd变形】
- HDOJ 题目Graph’s Cycle Component(并查集)
- HDU 4034 Graph(Floyd变形)
- HDOJ 题目1596 find the safest road(最短路Floyd)
- hdu 4034 Graph解题报告-Floyd思想
- HDOJ 题目2066一个人的旅行(最短路径,Floyd)
- HDU - 4034 Graph (floyd变形)
- HDU 4034 Graph(11年成都 Floyd运用)
- hdu 4034 Graph(floyd)
- BestCoder Round #53 (div.2) HDOJ5422 Rikka with Graph(floyd)
- hdu 4034 Graph (floyd)
- hdu 4034 Graph(floyd)
- HDOJ 题目1217 Arbitrage(最短路径,Floyd)