CDOJ 888 Absurdistan Roads

Absurdistan Roads

Time Limit: 5678/3456MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others)

 

The people of Absurdistan discovered how to build roads only last year. After the discovery, every city decided to build their own road connecting their city with another city. Each newly built road can be used in both directions.

Absurdistan is full of surprising coincidences. It took all N cities precisely one year to build their roads. And even more surprisingly, in the end it was possible to travel from every city to every other city using the newly built roads.

You bought a tourist guide which does not have a map of the country with the new roads. It only contains a huge table with the shortest distances between all pairs of cities using the newly built roads. You would like to know between which pairs of cities there are roads and how long they are, because you want to reconstruct the map of the N newly built roads from the table of shortest distances.

You get a table of shortest distances between all pairs of cities in Absurdistan using the N roads built last year. From this table, you must reconstruct the road network of Absurdistan. There might be multiple road networks with N roads with that same table of shortest distances, but you are happy with any one of those networks.

Input

For each test case:

  • A line containing an integer N (2N2000) -- the number of cities and roads.
  • N lines with N numbers each. The j-th number of the i-th line is the shortest distance from city i to city j. All distances between two distinct cities will be positive and at most 1000000. The distance from i to i will always be 0 and the distance from i to j will be the same as the distance from j to i.

Output

For each test case:

  • Print N lines with three integers 'a b c' denoting that there is a road between cities 1aN and 1bN of length 1c1000000, where ab. If there are multiple solutions, you can print any one and you can print the roads in any order. At least one solution is guaranteed to exist.

Print a blank line between every two test cases.

 

Sample input and output

Sample InputSample Output
4
0 1 2 1
1 0 2 1
2 2 0 1
1 1 1 0
4
0 1 1 1
1 0 2 2
1 2 0 2
1 2 2 0
3
0 4 1
4 0 3
1 3 0
2 1 1
4 1 1
4 2 1
4 3 1

2 1 1
3 1 1
4 1 1
2 1 1

3 1 1
2 1 4
3 2 3

 

 

 

 

 

 

 

 

Source

Northwestern European Regional Contest 2013
 
解题:先最小生成树一遍,然后求Floyd。看看是否都满足输入的方阵,如不满足则加边。代码思路很简单。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 2010;
18 struct arc{
19     int u,v,w;
20     arc(int x = 0,int y =  0,int z = 0){
21         u = x;
22         v = y;
23         w = z;
24     }
25 };
26 int mp[maxn][maxn],tot,n,cnt;
27 arc e[2000000];
28 int uf[maxn];
29 int Find(int x){
30     if(x != uf[x])
31         uf[x] = Find(uf[x]);
32     return uf[x];
33 }
34 bool cmp(const arc &x,const arc &y){
35     return x.w < y.w;
36 }
37 int kruskal(){
38     int i,j,k,u,v,w;
39     for(i = 1; i <= n; i++) uf[i] = i;
40     sort(e,e+tot,cmp);
41     for(cnt = i = 0; i < tot; i++){
42         int x = Find(e[i].u);
43         int y = Find(e[i].v);
44         if(x != y){
45             uf[x] = y;
46             u = e[i].u;
47             v = e[i].v;
48             w = e[i].w;
49             mp[e[i].u][e[i].v] = mp[e[i].v][e[i].u] = e[i].w;
50             cnt++;
51             printf("%d %d %d
",e[i].u,e[i].v,e[i].w);
52             if(cnt >= n-1) break;
53         }
54     }
55     for(k = 1; k <= n; k++){
56         for(i = 1; i <= n; i++){
57             for(j = 1; j <= n; j++){
58                 if(mp[i][k] == INF) break;
59                 if(mp[i][j] > mp[i][k]+mp[k][j]) mp[i][j] = mp[i][k]+mp[k][j];
60             }
61         }
62     }
63     for(i = 0; i < tot; i++){
64         if(e[i].w != mp[e[i].u][e[i].v]){
65             printf("%d %d %d
",e[i].u,e[i].v,e[i].w);
66             break;
67         }
68     }
69     if(i == tot) printf("%d %d %d
",u,v,w);
70 }
71 int main() {
72     int i,j,w;
73     bool flag = false;
74     while(~scanf("%d",&n)){
75         tot = 0;
76         if(flag) puts("");
77         flag = true;
78         for(i = 1; i <= n; i++){
79             for(j = 1; j <= n; j++){
80                 scanf("%d",&w);
81                 mp[i][j] = INF;
82                 if(j > i) e[tot++] = arc(i,j,w);
83             }
84         }
85         kruskal();
86     }
87     return 0;
88 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3944422.html