题目:http://118.190.20.162/view.page?gpid=T18
#include <iostream>
#include <algorithm>
#include <algorithm>
using namespace std;
int father[1010];
struct Node{
int x,y;
int cost;
}E[100010];
int find(int x){
return x==father[x]?x:father[x]=find(father[x]);
}
void merge(int x,int y){
int xx=find(x),yy=find(y);
if(xx!=yy) {
father[xx]=yy;
}
}
bool cmp(Node a,Node b){
return a.cost<b.cost;
}
int main() {
for(int i=1;i<=1000;i++){
father[i]=i;
}
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>E[i].x>>E[i].y>>E[i].cost;
}
sort(E,E+m,cmp);
int sum=0;
for(int i=0;i<m;i++){
int x=find(E[i].x),y=find(E[i].y);
if(x!=y){
merge(E[i].x,E[i].y);
sum+=E[i].cost;
}
}
cout<<sum<<endl;
return 0;
}