hdoj1233 还是畅通工程
2012-08-17 23:59
429 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233
最小生成树问题:prim算法
最小生成树问题:prim算法
#include <iostream> #include <algorithm> using namespace std; struct Road { int c1, c2, cost; }; Road road[5051]; int city[101];//统一将大序号指向小序号,形成树的根可由最小序号充当 int cmp(const Road a, const Road b) { return a.cost < b.cost; } int Findnext(const int n) {//查找与之相连接的城市 if (city == -1) return n; return city = Findnext(city );//压缩路径 } int Merge(const int a, const int b) {//合并 int x = Findnext(a); int y = Findnext(b); if (x == y) return 0; if (x < y) city[y] = x; else city[x] = y; return 1; } int main() { int n, m; int i, sum, count; // freopen("in.txt","r",stdin); while (cin >> n && n) { m = n*(n-1)/2; for (i = 0; i < m; i++) { cin >> road[i].c1 >> road[i].c2 >> road[i].cost; } sort(road, road+m, cmp); memset(city, -1, sizeof(city)); sum = 0; count = 0; for (i = 0; i < m; i++) { if (Merge(road[i].c1, road[i].c2)) { count ++; sum += road[i].cost; } if (count == n-1) break; } cout << sum << endl; } return 0; }
相关文章推荐
- [HDOJ1233]还是畅通工程
- hdoj 1233 还是畅通工程【最小生成树 kruskal && prim】
- HDOJ 1233 还是畅通工程(并查集)
- HDOJ---1233 还是畅通工程[Prim算法||Kruskal算法]
- hdoj-1233-还是畅通工程
- HDOJ 1233 还是畅通工程 【最小生成树】+【prim】
- 还是畅通工程hdoj1233
- HDoj-1233-还是畅通工程-prim算法
- HDOJ 1233 还是畅通工程
- HDOJ 1233 还是畅通工程 (最小生成树)
- 还是畅通工程 --HDOJ 1233
- hdoj 1233 还是畅通工程 【最小生成树】
- Num 32 : HDOJ : 1233 还是畅通工程 [ kruskal( 克鲁斯卡尔 )算法 ] [ 最小生成树 ]
- ACM HDOJ 1233 (还是畅通工程)
- HDOJ 还是畅通工程 1233
- hdoj problem 1233 还是畅通工程(并查集+动态规划)
- HDOJ 1233 还是畅通工程
- hdoj1233-还是畅通工程(并查集-kruskal && prim)
- 关于prim算法的讨论:例:HDOJ1233——还是畅通工程
- 【hdoj 1233】还是畅通工程