2 条题解
-
-16
AC
#include <bits/stdc++.h> #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define re register #define int long long using namespace std; typedef long long ll; struct node { int strt; int to; int len; }; bool cmp(node x, node y) { return x.len<y.len; } int n, m, x, y, z, ans; vector<node> edges; int parent[500010]; int fnd(int t) { return (parent[t]==t?t:parent[t]=fnd(parent[t])); } signed main() { IOS; cin>>n>>m; for(int i=1; i<=n; i++) parent[i]=i; for(int i=0; i<m; i++) cin>>x>>y>>z, edges.push_back({x, y, z}); sort(edges.begin(), edges.end(), cmp); for(int i=0; i<m; i++) { if(fnd(edges[i].strt)!=fnd(edges[i].to)) { parent[fnd(edges[i].strt)]=fnd(edges[i].to); ans+=edges[i].len; } } cout<<ans; return 0; }
信息
- ID
- 473
- 时间
- 1500ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 917
- 已通过
- 99
- 上传者