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 (2≤N≤2000) -- 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 1≤a≤N and 1≤b≤N of length 1≤c≤1000000, where a≠b. 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 Input Sample 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\n",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\n",e[i].u,e[i].v,e[i].w);
66             break;
67         }
68     }
69     if(i == tot) printf("%d %d %d\n",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 }

时间: 2024-08-02 07:00:15

CDOJ 888 Absurdistan Roads的相关文章

UESTC 888 Absurdistan Roads (kruscal+floyd)

(o(╯□╰)o还是很不习惯国外的区域赛题目...读不是很懂啊不是很懂啊...要研究半天) Absurdistan Roads Time Limit: 5678/3456MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others) Submit  Status The people of Absurdistan discovered how to build roads only last year. After the disco

UVAlive 6622 Absurdistan Roads(最小生成树+LCA)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4633 思路:每个点之间连边,权值为两点之间的最短距离.则该图的最小生成树的n-1条边在最终的n条边内.则两点(i,j)之间距离为dist[i]+dist[j]-2*dist[ LCA(i,j) ](dist[i]表示根节点(设为1)到i节点的距离).若树上每点之间的

UVALive 6622 Absurdistan Roads

题意: n(2000)个点的图  给出它的最短路矩阵  用n条边构造出满足最短路矩阵的图  保证图连通且解存在 思路: 我们可以先保证图连通  那么需要n-1条边  联想到是不是最小生成树?? 可以这样想  假设abc点已经连通  现在考虑再加入到连通块中一个点比如d  如果d-b的距离是d到abc三个点中最短的  那么这条边一定要被选  因为如果不选d-b  假设选了d-a  那么d-a已经长于d-b了  所以d-b的距离将不永远得不到满足 这样我们就可以根据最小生成树选出n-1条边了  还差

UESTC-888-Absurdistan Roads(kruskal+floyd)

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

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities)

【树形dp】Rebuilding Roads

[POJ1947]Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11934   Accepted: 5519 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake las

POJ——T2421 Constructing Roads

http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24132   Accepted: 10368 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can con

HDU1102--Constructing Roads(最小生成树)

Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B