代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1010
int p[N];
int v,l;
struct Kruskal //存储边的信息
{
int a,b;
int value;
};
bool cmp(Kruskal a,Kruskal b)
{
return a.value<b.value;
}
int getpa(int pa)
{
if(pa!=father[pa])
father[pa]=getpa(father[pa]);
return pa;
}
bool mercy(int a,int b)
{
int p1=getpa(a);
int p2=getpa(b);
if(p1==p2)
return false;
else
father[p1]=p2;
return true;
}
int main()
{
int T,lotal,sum,flag,i,j;
Kruskal dege[N];
cin>>T;
while(T--){
lotal=0,sum=0,flag=0;
for(i=1;i<=v;i++){
father[i]=i;
}
for(i=1;i<=l;i++){
cin>>dege[i].a>>dege[i].b>>dege[i].value;
}
sort(dege+1,dege+l+1,cmp);
for(i=1;i<=l;i++){
if(mercy(dege[i].a,dege[i].b)){
lotal++;
sum+=dege[i].value;
}
if(lotal=v=1)
{
flag=1;
break;
}
}
cout<<sum<<endl;
}
}
附加一点此刻的感悟: