这题克鲁斯卡尔就行 #include<bits/stdc++.h> using namespace std; vector<int> father; int find(int x){ return father[x] == x ? x : father[x] = find(father[x]); } bool cmp(const vector<int>&; a, const vector<int>&; b){ return a[2] < b[2]; } int main(){ int n, m; cin>>n>>m; father = vector<int>(n + 1); for(int i = 1; i <= n; i++) father[i] = i; vector<vector<int>> edges(m, vector<int>(3)); for(int i = 0; i < 3; i++){ for(int j = 0; j < m; j++) cin>>edges[j][i]; } sort(edges.begin(), edges.end(), cmp); int ans = 0; for(int i = 0; i < m; i++){ int f1 = find(edges[i][0]), f2 = find(edges[i][1]); if(f1 == f2) continue; ans += edges[i][2]; int ff = min(f1, f2); father[f1] = father[f2] = ff; } cout<<ans<<endl; return 0; }
3 1

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
牛客网
牛客企业服务