hdoj1863 畅通工程
2012-08-18 11:16
295 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863
此题与hdoj1233解法一样/article/8454391.html
此题与hdoj1233解法一样/article/8454391.html
#include <iostream> #include <algorithm> using namespace std; int city[101]; struct Road { int c1, c2, cost; }; Road road[5051]; int cmp(const Road a, const Road b) { return a.cost < b.cost; } int Find(const int n) { if (city == -1) return n; return city = Find(city ); } int Merge(const int a, const int b) { int x = Find(a); int y = Find(b); if (x == y) return 0; if (x < y) city[y] = x; else city[x] = y; return 1; } int main() { int n, m; int sum, count, i; // freopen("in.txt", "r", stdin); while (cin >> n >> m && n) { for (i = 0; i < n; i++) { cin >> road[i].c1 >> road[i].c2 >> road[i].cost; } sort(road, road+n, cmp); memset(city, -1, sizeof(city)); sum = count = 0; for (i = 0; i < n; i++) { if (Merge(road[i].c1, road[i].c2)) { count ++; sum += road[i].cost; } if (count == m-1) break; } if (count != m-1) cout << "?" << endl; else cout << sum << endl; } return 0; }
相关文章推荐
- ACM HDOJ 1863 (畅通工程)
- HDOJ 1863 畅通工程——克鲁斯卡尔算法
- [HDOJ1863]畅通工程
- HDOJ 畅通工程 1863(并查集+prim)
- HDOJ 1863 畅通工程 (Kruskal 最小生成树)
- hdoj 1863 畅通工程
- hdoj 1863 畅通工程
- HDOJ 1863 畅通工程 (最小生成树)
- hdoj 1863 畅通工程 【最小生成树】
- 最小生成树——HDOJ 1863 畅通工程 解题报告
- hdoj 1863 畅通工程 最小生成树---prime算法
- Kruskal HDOJ 1863 畅通工程
- hdoj 1863 畅通工程 【最小生成树】+【kruskal】
- Num 31 : HDOJ : 1863 畅通工程 [ kruskal( 克鲁斯卡尔 )算法 ] [ 最小生成树 ]
- HDOJ1863 畅通工程(Kruskal算法)
- HDOJ 1863 畅通工程(Kruskal最小生成树)
- HDOJ 1863 畅通工程(并查集)
- hdoj 1863 畅通工程
- hdoj 1863 畅通工程
- HDOJ1863(畅通工程)