Connect them
Time limit:1 second Memory limit:32768 KB
You haveNComputers numbered from 1NAnd you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computersIAndJIs the same as connecting computersJAndI). The cost of connecting computerIAnd computerJIsCIJ. You cannot connect some pairs of computers due to some special reasons. you want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.
GivenNAnd eachCIJ, Find the cheapest way to connect computers.
Input
There are multiple test cases. The first line of input contains an integerT(T<= 100), indicating the number of test cases. ThenTTest Cases follow.
The first line of each test case contains an integerN(1 <N<= 100). ThenNLines follow, each of which containsNIntegers separated by a space.J-Th INTEGER OFI-Th line in theseNLines isCIJ, Indicating the cost of connecting computersIAndJ(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, output the method in the following fomat:
I1J1I1J1 ......
WhereIk Ik(K> = 1) are the identification numbers of the two computers to be connected. all the integers must be separated by a space and there must be no extra space at the end of the line. if there are multiple solutions, outputLexicographically smallestOne (see hints for the definition"Lexicography small") If you cannot connect them, just output"-1 "in the line.
Sample Input
230 2 32 0 53 5 020 00 0
Sample output
1 2 1 3-1
Hints:
A solutionAIs a linePIntegers:A1,A2,...AP.
Another solutionBDifferent fromAIs a lineQIntegers: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
The minimum spanning tree requires the solution of the smallest Lexicographic Order.
Use kruscalAlgorithm, Sort first, and also sort the output.
/* Zoj 3204 calculates the minimum number of lexicographic orders */ # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> Using Namespace STD; Const Int Maxn = 110 ; Int F [maxn]; Struct Edge { Int From ,; Int W;} edge [maxn * Maxn]; Int Tol; edge ans [maxn * Maxn]; Int CNT; Void Addedge ( Int U, Int V, Int W) {edge [tol]. From = U; edge [tol]. = V; edge [tol]. W = W; Tol ++ ;} Bool CMP1 (edge a, edge B ){ If (A. W! = B. W) Return A. W < B. W; Else If (. From ! = B. From ) Return A. From <B. From ; Else Return A. to < B. ;} Bool Cmp2 (edge a, edge B ){ If (. From ! = B. From ) Return A. From <B. From ; Else Return A. to < B. ;} Int Find ( Int X ){ If (F [x] =- 1 ) Return X; Return F [x] = Find (F [x]);} Void Kruscal () {memset (F, - 1 , Sizeof (F); CNT = 0 ; // Edge added to the Minimum Spanning Tree For (Int K = 0 ; K <tol; k ++ ){ Int U = edge [K]. From ; Int V = Edge [K].; Int T1 = Find (U ); Int T2 = Find (v ); If (T1! = T2) {ans [CNT ++] = Edge [k]; F [T1] = T2 ;}}} Int Main (){ Int T; scanf ( " % D " ,& T ); Int N; 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);} Sort (edge, Edge + Tol, CMP1); kruscal (); If (CNT! = N- 1 ) {Printf ( " -1 \ n " ); Continue ;} Else {Sort (ANS, ANS + CNT, cmp2 ); For ( Int I = 0 ; I <CNT-1 ; I ++ ) Printf ( " % D " , ANS [I]. From , ANS [I]. To); printf ( " % D \ n " , ANS [CNT- 1 ]. From , ANS [CNT- 1 ]. );}} Return 0 ;}