传送门:https://www.luogu.org/problemnew/show/P3366
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct cc
{
int val;
int to;
int ss;
}a[200010];
int f[5010];
int n,m;
int pp,xx;
bool cmp(cc x,cc y)
{
return x.val<y.val;
}
int find(int t)
{
if(f[t]==t)return f[t];
return f[t]=find(f[t]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[i].to=y;
a[i].val=z;
a[i].ss=x;
}
sort(a+1,a+1+m,cmp);
for(int i=1;i<=n;i++)
{
f[i]=i;
}
for(int i=1;i<=m;i++)
{
int v=find(a[i].to);
int t=find(a[i].ss);
if(v!=t)
{
xx+=a[i].val;
f[v]=t;
pp++;
if(pp==n-1)break;
}
}
if(pp!=n-1)
{
cout<<"orz";
return 0;
}
cout<<xx;
return 0;
}