ZJU 3204 最小生成树+路径保存


题意:给你一个N*N的邻接矩阵,aij表示节点i和j的联通权值为aij,求最小生成树的路径




#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define inf -0x3f3f3f3f
#define eps 1e-8
#define LL long long
#define M 100001
#define mol 100000000

struct Edge
{
	int from,to,v;
}edge[110*100], path[110*100];

int fa[110];

bool cmp(Edge a,Edge b)
{
	if(a.v==b.v)
	{
		if(a.from ==b.from )
			return a.to <b.to ;
		return a.from <b.from ;
	}
	return a.v <b.v;
}

bool cmp2(Edge a,Edge b)
{
	if(a.from ==b.from ) return a.to <b.to ;
	return a.from <b.from ;
}
int find(int x)
{

	if(x==fa[x]) return x;
	return fa[x]=find(fa[x]);
}

int main()
{
	int t,n,i,j,a;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int num=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				scanf("%d",&a);
				if(a!=0)
				{
					edge[num].from = i;
					edge[num].to = j;
					edge[num++].v = a;
				}
			}
		}
		sort(edge,edge+num,cmp);
		for(i=0;i<=n;i++) fa[i]=i;
		int x,y,j=0;
		for(i=0;i<num;i++)
		{
			x=find(edge[i].from );
			y=find(edge[i].to );
			if(x!=y)
			{
				fa[x]=y;
				path[j++]=edge[i];
			}
		}
		if(j!=n-1) puts("-1");
		else
		{
			sort(path,path+j,cmp2);
			printf("%d %d",path[0].from ,path[0].to );
			for(i=1;i<j;i++)
				printf(" %d %d",path[i].from ,path[i].to );
			printf("\n");
		}
	}
	return 0;
}


你可能感兴趣的:(ZJU 3204 最小生成树+路径保存)