16 条题解
-
0
#include<bits/stdc++.h> #define int long long #define endl '\n' #define pr(x) pair<x,x> #define up(i,j,k,l) for(int i=j;i<=k;i+=l) #define down(i,j,k,l) for(int i=j;i>=k;i-=l) using namespace std; const int N=2e5+10; int n,m; struct node{ int u,v,w; }; node a[N]; int f[N],t1,t2,ans; int fd(int x) { if(x==f[x]){ return x; } else{ return f[x]=fd(f[x]); } } bool cmp(node x1,node x2) { return x1.w<x2.w; } void solve() { cin>>n>>m; up(i,1,n,1){ f[i]=i; } up(i,1,m,1){ cin>>a[i].u>>a[i].v>>a[i].w; } sort(a+1,a+m+1,cmp); up(i,1,m,1){ t1=fd(a[i].u); t2=fd(a[i].v); if(t1!=t2){ f[t1]=t2; ans+=a[i].w; } } cout<<ans<<endl; return; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); int _=1; //cin>>_; up(i,1,_,1){ solve(); } return 0; }
信息
- ID
- 91
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 944
- 已通过
- 319
- 上传者