首页 > 其他 > 详细

hdu4034 Graph(floyd)

时间:2014-06-18 22:37:31      阅读:362      评论:0      收藏:0      [点我收藏+]

题目:  给出一个图的最短路,求原图最少几条边

Graph

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


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
 
 
 
先假设每两个点之间都有边,ans = n*(n-1), 然后用floyd , 对于 i,j , 以k为中点,如果 dist[i][j] == dist[i][k]+dist[k][j] 说明这条路可以被替代,减去一条边。
反之如果大于的话,就和题目的最短路矛盾了。 输出impossible
 
代码:
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <sstream>
 3 #include <ios>
 4 #include <iomanip>
 5 #include <functional>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <string>
 9 #include <list>
10 #include <queue>
11 #include <deque>
12 #include <stack>
13 #include <set>
14 #include <map>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cmath>
18 #include <cstring>
19 #include <climits>
20 #include <cctype>
21 using namespace std;
22 #define XINF INT_MAX
23 #define INF 0x3FFFFFFF
24 #define MP(X,Y) make_pair(X,Y)
25 #define PB(X) push_back(X)
26 #define REP(X,N) for(int X=0;X<N;X++)
27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
29 #define CLR(A,X) memset(A,X,sizeof(A))
30 #define IT iterator
31 typedef long long ll;
32 typedef pair<int,int> PII;
33 typedef vector<PII> VII;
34 typedef vector<int> VI;
35 
36 int N;
37 int dist[105][105];
38 int vis[105][105];
39 int main()
40 {
41     ios::sync_with_stdio(false);
42 
43     int tst;
44     cin>>tst;
45     int cse = 0;
46     while(tst--)
47     {
48         CLR(dist,0);CLR(vis,0);
49 
50         cin>>N;
51         REP(i,N)
52         REP(j,N)
53         {
54             cin>>dist[i][j];
55         }
56         cout<<"Case "<<++cse<<": ";
57 
58         int ans = N*(N-1);
59         REP(k,N)
60         {
61             REP(i,N)
62             {
63                 REP(j,N)
64                 {
65                     if( i == k || j == k)continue;
66                     if(!vis[i][j] && dist[i][j] == dist[i][k]+dist[k][j])
67                     {
68                         ans--;
69                         vis[i][j] =1;
70                     }
71                     if( dist[i][j] > dist[i][k]+dist[k][j])
72                     {
73                         ans = -1;
74                         break;
75                     }
76                 }
77                 if( ans ==-1)
78                 {
79                     break;
80                 }
81             }
82             if(ans==-1)break;
83         }
84         if( ans == -1)
85         {
86             cout<<"impossible"<<endl;
87         }
88         else
89         {
90             cout<<ans<<endl;
91         }
92 
93     }
94 
95     return 0;
96 }
View Code

 

 
 

hdu4034 Graph(floyd),布布扣,bubuko.com

hdu4034 Graph(floyd)

原文:http://www.cnblogs.com/doubleshik/p/3790154.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!