解题思路:最小生成树模版题
注意事项:
参考代码:
#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 人评分
C二级辅导-求偶数和 (C语言代码)浏览:639 |
母牛的故事 (C语言代码)浏览:1716 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:692 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:581 |
C语言训练-角谷猜想 (C语言代码)浏览:1705 |
【亲和数】 (C语言代码)浏览:541 |
A+B for Input-Output Practice (VI) (C++代码)浏览:417 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:572 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:647 |
简单的a+b (C语言代码)浏览:531 |