专题四 · 1004
来源:互联网 发布:怎样在淘宝网上开店铺 编辑:程序博客网 时间:2024/05/19 22:01
代码及解释
#include <iostream>#include <algorithm>// 刚刚考完计算机组成,赶快做一道题��// 连续好几道 Kruskal,代码几乎都不用改(感谢老师��)//using std::cin;using std::cout;using std::endl;const int N = 200;int set[N];int find_f(int x) { if (x != set[x]) set[x] = find_f(set[x]); return set[x];}struct edge { int x, y, v;} e[N*(N - 1) / 2];inline int cmp(edge e1, edge e2) { return e1.v<e2.v;}int main() { int n; while (cin >> n && n) { for (int i = 0; i <= n; ++i) set[i] = i; n = n * (n - 1) / 2; for (int i = 0; i<n; i++) cin >> e[i].x >> e[i].y >> e[i].v; std::sort(e, e + n, cmp); int ans = 0; for (int i = 0; i < n; ++i) { int x = find_f(e[i].x); int y = find_f(e[i].y); if (x != y) { ans += e[i].v; set[x] = y; } } cout << ans << endl; } return 0;}
0 0
- 专题四 · 1004
- 专题四1004
- 专题四1004
- 1004-D专题四
- 专题四 · 1001
- 专题四 · 1002
- 专题四 · 1003
- 专题四 · 1006
- 专题四 · 1005
- 专题四 · 1008
- 专题四 · 1009
- 专题四 · 1010
- 专题四 · 1011
- 杭电ACM专题四1004
- 3G专题四
- LDAP专题(四)
- 专题四1003
- 专题四1003
- Shell编程之条件判断和流程控制
- 我和Google的故事
- HeadFirstJava——2_类与对象
- flume集群搭建
- Linux下线程的调度策略与优先级(二)
- 专题四 · 1004
- 工作顺心才能有更好的未来
- 20个常识之最-不要说你5个都说不出来
- POI2012题解
- Fragment异常: Error inflating class fragment
- Android应用层源码 进阶
- nyoj 19 擅长排列的小明
- xcode 编译静态库所要注意
- Xcode运行时不能选择模拟器