UESTC-888-Absurdistan Roads (kruskal + floyd), kruskal Algorithm
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 ctions.
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 wowould like to know between which pairs of cities there are roads and how long they are, because you want to reconstruct the map of 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 N Roads built last year. From this table, you must reconstruct the road network of Absurdistan. There might be multiple road networks 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 N Numbers each. J -Th number of 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 |
40 1 2 11 0 2 12 2 0 11 1 1 040 1 1 11 0 2 21 2 0 21 2 2 030 4 14 0 31 3 0 |
2 1 14 1 14 2 14 3 12 1 13 1 14 1 12 1 13 1 12 1 43 2 3 |
SourceNorthwestern European Regional Contest 2013
Idea: first use kruskal to find n-1 edges, then n-1 edges must be satisfied. Next, you only need to find another edge, you can enumerate all edges from small to large by weight until the distance between an edge and the distance between the first n-1 edges in the graph is not equal, if not, output any of the first n-1 edges.
# Include <stdio. h >#include <algorithm> # define INF 999999999 using namespace std; struct E {int u, v, val; bool operator <(const E & p) const {return val <p. val ;}} e [4000005]; int node [2005], dis [2005] [2005]; int findroot (int x) {if (node [x]! = X) return node [x] = findroot (node [x]); return node [x];} int main () {int n, I, j, k, t, u, v, val, cnt, roota, rootb; bool first = 1; while (~ Scanf ("% d", & n) {if (first) first = 0; else puts (""); cnt = 0; for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {scanf ("% d", & val ); if (I <= j) continue; e [cnt]. u = I; e [cnt]. v = j; e [cnt ++]. val = val;} sort (e, e + cnt); for (I = 1; I <= n; I ++) node [I] = I; for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) dis [I] [j] = INF; t = 0; for (I = 0; I <cnt; I ++) {roota = findroot (e [I]. u); rootb = findroot (e [I]. v); if (roota! = Rootb) {node [roota] = rootb; dis [e [I]. u] [e [I]. v] = dis [e [I]. v] [e [I]. u] = e [I]. val; u = e [I]. u, v = e [I]. v, val = e [I]. val; printf ("% d \ n", e [I]. u, e [I]. v, e [I]. val); t ++; if (t> = N-1) break ;}}for (k = 1; k <= n; k ++) {for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {if (dis [I] [k] = INF) break; // no such optimization is available... Dis [I] [j] = min (dis [I] [j], dis [I] [k] + dis [k] [j]) ;}} for (I = 0; I <cnt; I ++) {if (e [I]. val! = Dis [e [I]. u] [e [I]. v]) {printf ("% d \ n", e [I]. u, e [I]. v, e [I]. val); break ;}}if (I = cnt) printf ("% d \ n", u, v, val );}}
Kruskal (pascal)
This is a version that does not need to be checked.
Var
X: array [1 .. 100, 1 .. 3] of integer;
N, xlen: integer;
S: longint;
Procedure init;
Var I, j, t, k: integer;
Begin
Fillchar (x, sizeof (x), 0 );
Assign(input,'prim.txt ');
Reset (input );
Readln (n); k: = 0;
For I: = 1 to n do
Begin
For j: = 1 to I do
Begin
Read (t );
If t> 0 then
Begin inc (k); x [k, 1]: = I; x [k, 2]: = j; x [k, 3]: = t; end;
End;
For j: = I + 1 to n do
Read (t );
End;
Close (input );
Xlen: = k;
For I: = 1 to xlen-1 do
Begin
K: = I;
For j: = I + 1 to xlen do
If x [j, 3] <x [k, 3] then
K: = j;
T: = x [I, 1]; x [I, 1]: = x [k, 1]; x [k, 1]: = t;
T: = x [I, 2]; x [I, 2]: = x [k, 2]; x [k, 2]: = t;
T: = x [I, 3]; x [I, 3]: = x [k, 3]; x [k, 3]: = t;
End;
End;
Procedure main;
Var
Y: array [1 .. 100] of set of 0 .. 255;
I, j: integer;
P1, p2: integer;
Begin
For I: = 1 to xlen do
Y [I]: = [];
For I: = 1 to xlen do
Begin
P1: = 0; p2: = 0;
For j: = 1 to xlen do
Begin
If p1 = 0 then
If x [I, 1] in y [j] then
P1: = j;
If p2 = 0 then
If x [I, 2] in y [j] then
P2: = j;
End;
If p1 + p2 = 0 then
Begin y [I]: = [x [I, 1] + [x [I, 2]; s: = s + x [I, 3]; end;
If (p1> 0) and (p2 = 0) then
Begin y [p1]: = y [p1] + [x [I, 2]; s: = s + x [I, 3]; end;
If (p1 = 0) and (p2> 0) then
... The remaining full text>
For an undirected graph containing n vertices and e-edge, the time complexity of the Kruskal algorithm of the Minimum Spanning Tree is ()
The time complexity of the kruskal algorithm is mainly determined by the sorting method. The Sorting Algorithm is only connected to a network with n vertices with the weight edge, TV is the set of vertices in the Minimum Spanning Tree on WN. TE