ZOJ 3204 Connect them (minimum spanning tree: kruscal algorithm)

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204


Connect them Time Limit: 1 Second Memory Limit:32768 KB

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 computersI andJ is the same as connecting ComputersJ andi). The cost of connecting computerI and computerJ 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 integert (T <=), indicating the number of test cases. ThenT test Cases follow.

The first line of all test case contains an integern (1 <n <= 100). Thenn lines follow, each of the which containsn integers separated by a space. TheJ-th Integer of theI-th line in thesen lines are CIJ, indicating the cost of C onnecting ComputersI andJ (Cij = 0 means that you cannot connect them). 0 <=CIJ <= 60000,cij =cji,cii = 0, 1 <=i, J & lt;=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 solution a is a line of p integers: A1, A2, ... ap.
Another solution B different from a was a line of q integers: b1, B2, ... C13>BQ.
A is lexicographically smaller than B if and only if:
(1) There exists a positive integer r ( r <= p, r <= q) such th At ai = bi for all 0 < I < R and ar < BR
OR
(2) P < q and ai = bi for all 0 < i <= p


The main topic: there are several computers, how to use the least cost of his

Connect them.

Problem solving: minimum spanning tree. First, the edges are sorted from small to large, followed by

The points at both ends of the edge are combined with a check set. It is important to note that the topic

The given matrix on the triangle and the lower triangle is the same, that is, just deal with

Half of it is OK.


<span style= "FONT-SIZE:24PX;" >///Knowledge Points: Minimum spanning tree #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace Std; const int maxn=110; int F[MAXN];     struct Edge {int from,to; 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].to=v;     Edge[tol].w=w; tol++;     } bool Cmp1 (Edge A,edge b) {if (A.W!=B.W) return a.w<b.w;     else if (A.from!=b.from) return a.from<b.from; else return a.to<b.to;     } bool Cmp2 (Edge A,edge b) {if (A.from!=b.from) return a.from<b.from; else return a.to<b.to;     } 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;         for (int k=0;k<tol;k++) {int u=edge[k].from;         int v=edge[k].to;         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%d", ans[i].from,ans[i].to);         printf ("%d%d\n", ans[cnt-1].from,ans[cnt-1].to); }} return 0; }</span>



Copyright NOTICE: This article for Bo Master original article, reprint remember famous source, thank you!

ZOJ 3204 Connect them (minimum spanning tree: kruscal algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.