Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367
You had n computers numbered from 1 to n and you want to connect them to make a small the local area networ K (LAN). All connections be two-way (that's connecting computers I and J is the same as connecting computers j and i). The cost of connecting computer I and computer J are CIJ. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so, every computer connects to any other one directly or indirectly and you also want As little as possible.
Given n and each CIJ , find the cheapest-to connect computers.
Input
There is multiple test cases. The first line of input contains an integer t (T <=), indicating the number of test cases. Then T test Cases follow.
The first line of all test case contains an integer n (1 < n <= 100). Then n lines follow, each of the which contains n integers separated by a space. The J-th Integer of theI-th line in these n lines are CIJ, indicating the cost of Conne cting Computers I and J (Cij = 0 means that you cannot connect them). 0 <= CIJ <= 60000, cij = cji, cii = 0, 1 <= i,J <= n.
Output
For each test case, if you can connect the computers together, the output of the method in the the following fomat:
I1 J1 i1 J1 ...
where ik ik (k >= 1) is the identification numbers of the computers to be connected. All the integers must is separated by a space and there must is no extra space at the end of the line. If There is multiple solutions, output the lexicographically smallest one (see hints for the definition of " Lexicography small") If You cannot connect them, just output"-1 "on the line.
Sample Input
230 2 32 0 53 5 020 00 0
Sample Output
1 2 1 3-1
Hints:
A SolutionAis a line ofPIntegers:A1,A2, ...AP.
Another solutionBDifferent fromAis a line ofQIntegers:B1,B2, ...BQ.
AIslexicographically smallerthanBIf and only if:
(1) There exists a positive integerR(R<=P,R<=Q) such thatAI=BiFor all 0 <I<Randar<BR
OR
(2)P<QandAI=BiFor all 0 <I<=P
Author:CAO, Peng
Source:The 6th Zhejiang Provincial Collegiate Programming Contest
Test instructions
Smallest dictionary order in the output minimum spanning tree!
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define MAXN 147int f[maxn];struct edge{int u, v; int W;} EDGE[MAXN*MAXN], ans[maxn*maxn];int n;int tol;int cnt;void addedge (int u,int v,int w) {edge[tol].u = u; EDGE[TOL].V = v; EDGE[TOL].W = W; tol++;} BOOL Cmp1 (Edge A,edge B)//Guaranteed to return A.W < B.W in dictionary order from small output {if (a.w! = B.W); else if (a.u! = b.u) return a.u < b.u; else return A.V < B.V;} BOOL Cmp2 (Edge A,edge b) {if (a.u! = b.u) return a.u < b.u; else return A.V < B.V;} int find (int x) {return x = = F[x]? X:f[x]=find (F[x]);} void Kruscal () {for (int i = 0; I <= N; i++) {f[i] = i; } sort (EDGE,EDGE+TOL,CMP1);//The Order of the edges cnt = 0; for (int i = 0; i < tol; i++) {int u = edge[i].u; int v = EDGE[I].V; int T1 = find (u); int t2 = Find (v); if (t1! = t2) {ans[cnt++] = edGe[i]; F[T1] = T2; }}}int Main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); Tol = 0; int W; for (int i = 1; I <= n; i++) for (int j = 1; J <= N; j + +) {scanf ("%d", &w); if (J <= i) continue; if (w = = 0) continue; Addedge (I,J,W); } kruscal (); if (cnt!=n-1) {printf (" -1\n"); } else {sort (ANS,ANS+CNT,CMP2); for (int i = 0; i < cnt-1; i++) printf ("%d%d", ANS[I].U,ANS[I].V); printf ("%d%d\n", ANS[CNT-1].U,ANS[CNT-1].V); }} return 0;}
ZOJ 3204 Connect them (smallest spanning tree krusal output dictionary order)