zoj3204 Connect them 最小生成树

时间:2023-03-08 23:22:38
zoj3204  Connect them  最小生成树

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367

题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一,答案要求输出字典序最小

代码:

 #include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 110
int n,tol;
int cnt;
class node
{
public:
int from;
int to;
int w;
};
node edge[maxn*maxn];
node ans[maxn*maxn];
int parent[maxn*maxn];
void addedge(int u,int v,int w)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].w=w;
tol++;
}
void UFset()
{
for(int i=;i<maxn*maxn;i++)
parent[i]=-;
}
int Find( int x)
{
int s;
for(s=x;parent[s]>=; s=parent[s]);
while(s!=x)
{
int tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void Union(int R1, int R2)
{
int root1=Find(R1);
int root2=Find(R2); int tmp=parent[root1]+parent[root2]; if(parent[root1]> parent[root2])
{
parent[root1]=root2;
parent[root2]=tmp;
}
else
{
parent[root2]=root1;
parent[root1]=tmp;
}
}
bool cmp1( node a, node 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(node a, node b)
{
if(a.from!=b.from)return a.from<b.from;
else return a.to<b.to;
}
void Kruskal()
{
int num=;
int u,v;
UFset();
cnt=; for(int i=;i<tol;i++)
{
u=edge[i].from;
v=edge[i].to;
if(Find(u) != Find(v))
{
ans[cnt++]=edge[i];
Union(u,v);
} }
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
tol=;
int weight;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&weight);
if(j<=i) continue;
if(weight== ) continue;
addedge(i,j,weight);
}
sort(edge,edge+tol,cmp1); Kruskal(); if(cnt!=n-)
cout<<"-1"<<endl;
else
{
sort(ans,ans+cnt,cmp2);
cout<<ans[].from<<" "<<ans[].to;
for(int i=;i<cnt;i++)
cout<<" "<<ans[i].from<<" "<<ans[i].to;
cout<<endl;
}
}
return ;
}