标签:
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
using namespace std;
struct road{
int from;
int to;
int weath;
};
road a[105];
int n,e,tot;
int r1,r2,father[105];
int cmp(const road&b,const road&c){
if(b.weath<c.weath)return 1;
else return 0;
}
int find(int x){
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}
void unionn(int r1,int r2){
father[r2]=r1;
}
int main(){
scanf("%d%d",&n,&e);
for(int i=1;i<=e;i++){
scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].weath);
}
for(int i=1;i<=n;i++){
father[i]=i;
}
sort(a+1,a+1+e,cmp);
for(int i=1;i<=n;i++){
tot++;
if(father[a[tot].from]!=father[a[tot].to]){
printf("%d %d\n",a[tot].from,a[tot].to);
r1=find(a[tot].from);
r2=find(a[tot].to);
if(r1!=r2)unionn(r1,r2);
}
else continue;
}
printf("Time used=%0.2lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/cdqzfredrick/p/5303287.html