Connect them ZOJ - 3204 (最小生成树)

You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular 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.

Given n and each cij , find the cheapest way to connect computers.

Input

There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.

The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computersi and j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji,cii = 0, 1 <= ij <= n.

<b< dd="">

Output

For each test case, if you can connect the computers together, output the method in in the following fomat:

i1 j1 i1 j1 ......

where ik 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, output thelexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.

<b< dd="">

Sample Input

2
3
0 2 3
2 0 5
3 5 0
2
0 0
0 0

<b< dd="">

Sample Output

1 2 1 3
-1

<b< dd="">

Hint
s:
A solution  A is a line of  p integers:  a1a2, ... ap.
Another solution  B different from  A is a line of  q integers:  b1b2, ... bq.
A is  lexicographically smaller than  B if and only if:
(1) there exists a positive integer  r ( r <=  pr <=  q) such that  ai =  bi for all 0 <  i <  r and  ar <  br 
OR
(2)  p <  q and  ai =  bi for all 0 <  i <=  p

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
struct node{
	int u;
	int v;
	int zhi;
};
struct luu{
	int x;
	int y;
};
luu lu[500];
bool cmp(node a,node b)
{
	if(a.zhi != b.zhi)return a.zhi < b.zhi;
	if(a.u!=b.u) return a.u < b.u;
	return a.v < b.v;
}
bool cmp2(luu aa,luu bb)
{
	if(aa.x == bb.x) return aa.y < bb.y;
	return aa.x < bb.x;
}
node op[10010];
int f[500];
int n;
int getf(int x)
{
	if(f[x]==x)return f[x];
	f[x] = getf(f[x]);
	return f[x];
}
bool merge(int x,int y)
{
	int xx = getf(x);
	int yy = getf(y);
	if(xx!=yy) 
	{
		f[yy] = xx;
		return true;
	}
	else return false;
}
int main()
{
  int t;
  scanf("%d", &t);
  while(t--)
  {
     int flag = 1;
     scanf("%d", &n); 
	 int k = 1; 	
  	 for(int i=1; i<=n; i++)
  	 {
  	 	for(int j=1; j<=n; j++)
  	 	{
  	 	  op[k].u = i;
  	 	  op[k].v = j;
  	 	  scanf("%d", &op[k].zhi);
  	 	  ++k;
	    }
     }
     --k;
  	  for(int i=1; i<=n; i++) f[i] = i;
  	  sort(op+1,op+1+k,cmp);
  	  memset(lu, 0, sizeof(lu));
  
  int w = 1;
  int bian = 0;
  for(int i=1; i<=k; i++)
  {
    if(op[i].zhi == 0)continue;
	if(merge(op[i].u,op[i].v))
  	{
  	  lu[w].x = op[i].u;
	  lu[w].y = op[i].v;
	    ++w;
	   bian ++;		
	}
  	if(bian == n-1)break;
  }
  --w;
  if(bian!=n-1)
  printf("-1\n");
  else 
  {
    sort(lu+1,lu+1+w,cmp2);
	for(int i=1; i<=w; i++) 
    {
    	if(i!=1)printf(" ");
    	printf("%d %d", lu[i].x,lu[i].y);
	}
    printf("\n");
  }
 } 
  return 0;
} 
水波.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值