1004-D专题四
来源:互联网 发布:360浏览器精简优化版 编辑:程序博客网 时间:2024/06/11 08:50
1,.题意:给出镇子之间距离,修路使它们联通,求最小的修路距离
2.思路:最小生成树问题,用的kruskal算法,跟第一个题类似
3.感想:觉得kruskal理解起来很简单,用起来也好用
#include <iostream>#include <algorithm>using namespace std;const int N = 105;int father[N];int find(int x){ if (x != father[x]) father[x] = find(father[x]); return father[x];}struct edge{ int x, y, v;}e[N*(N - 1) / 2];int cmp(edge e1, edge e2){ return e1.v<e2.v;}int main(){ int n,x,y; while (cin >> n&&n) { for (int i = 0; i <= n; ++i) father[i] = i; n = n*(n - 1) / 2; for (int i = 0; i<n; i++) cin>>e[i].x>>e[i].y>>e[i].v; sort(e, e + n, cmp); int ans = 0; for (int i = 0; i < n; ++i) { x = find(e[i].x); y = find(e[i].y); if (x != y) { ans += e[i].v; father[x] = y; } } cout << ans << endl; } return 0;}
0 0
- 1004-D专题四
- 专题四 Problem D
- 专题四 Problem D
- 1004-D专题三
- 专题四1004
- 专题四1004
- 专题四 · 1004
- [kuangbin带你飞]专题四 最短路练习D
- 杭电ACM专题四1004
- 自定义控件专题之四:带3d翻转特效的滑动页
- [kuangbin带你飞]专题四 最短路练习 D POJ 3268
- 数论专题训练D
- 专题三 Problem D
- 二分贪心专题D
- 搜索专题 D
- 【背包专题】D
- 【背包专题】 D
- 3G专题四
- ZOJ 3487A - Ordinal Numbers
- Python匹配对象与索引使用
- Date类,Calendar类,DateFormat类
- 简单易学的机器学习算法——梯度提升决策树GBDT
- PAT(乙级)1013. 数素数
- 1004-D专题四
- ZOJ 3499 M - Median
- android (六)、Service工作原理
- TextView实现分散对齐(两端对齐)
- Android中eclipse开虚拟机的问题
- 第129课:Spark streaming源码经典解读系统之四:GobGenerator工作内幕源码解密
- test
- ZOJ 3498 L - Javabeans
- meta标签