解题思路:最小生成树模版题
注意事项:
参考代码:
#include<bits/stdc++.h> using namespace std; int f[1000]; struct node { int from; int to; int w; }; int find(int x) { if(x!=f[x]) { return f[x]=find(f[x]); } return x; } long long ans=0; void meare(node a,bool t) { int x=find(a.from); int y=find(a.to); if(x!=y) { f[y]=x; if(t)ans+=a.w; } } vector<node>v; bool cmp(node a,node b) { return a.w<b.w; } int main() { int n; while(cin>>n&&n) { ans=0; for(int i=0;i<=n;i++) { f[i]=i; } for(int i=1;i<=(n*(n-1)/2);i++) { int d; node a; cin>>a.from>>a.to>>a.w; v.push_back(a); } sort(v.begin(),v.end(),cmp); for(int i=0;i<v.size();i++) { meare(v[i],1); } v.clear(); cout<<ans<<endl; } return 0; }
0.0分
1 人评分
简单的a+b (C++语言代码)浏览:853 |
字符串的输入输出处理 (C语言代码)浏览:924 |
WU-输出正反三角形 (C++代码)浏览:1018 |
数字游戏 (C++代码)浏览:1178 |
C语言训练-8除不尽的数 (C语言代码)浏览:1396 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:417 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:567 |
C语言训练-求素数问题 (C语言代码)浏览:611 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:303 |
Manchester- A+B for Input-Output Practice (II)浏览:1306 |