解题思路:最小生成树模版题
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:958 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:738 |
求组合数 (C语言代码)浏览:1152 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:691 |
WU-陶陶摘苹果2 (C++代码)浏览:967 |
蛇行矩阵 (C语言代码)浏览:524 |
1013题解浏览:552 |
Hello, world! (C语言代码)浏览:808 |
母牛的故事 (C语言代码)浏览:478 |
买不到的数目 (C语言代码)浏览:3089 |