hdu4034 Graph(floyd)

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

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
 
代码:
 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

 

 
 

转载于:https://www.cnblogs.com/doubleshik/p/3790154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值