HDOJ1233-还是畅通工程
2011-06-22 18:53
357 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1233
问题描述:
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
Sample Output
问题描述:
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
Sample Output
3 5 HintHint Huge input, scanf is recommended.#include <iostream> #include <algorithm> using namespace std; typedef struct Road { int c1, c2, cost; }Road; bool myCompare(const Road &a, const Road &b) { if(a.cost < b.cost) return 1; return 0; } Road road[5051]; int city[101]; int Find(int n) { if(city == -1) return n; return city = Find(city ); } bool Merge(int s1, int s2) { int r1 = Find(s1); int r2 = Find(s2); if(r1 == r2) return 0; if(r1 < r2) city[r2] = r1; else city[r1] = r2; return 1; } int main() { //freopen("input.txt", "r", stdin); int n; while(scanf("%d", &n) && n) { int m = n*(n-1)/2; memset(city, -1, sizeof(city)); for(int i=0; i<m; ++i) scanf("%d %d %d", &road[i].c1, &road[i].c2, &road[i].cost); sort(road, road+m, myCompare); int sum = 0, count = 0; for(int i=0; i<m; ++i) { if(Merge(road[i].c1, road[i].c2)) { count ++; sum += road[i].cost; } if(count == n-1) break; } printf("%d/n", sum); } return 0; }
相关文章推荐
- [HDOJ1233]还是畅通工程
- HDOJ---1233 还是畅通工程[Prim算法||Kruskal算法]
- hdoj 1233 还是畅通工程【最小生成树 kruskal && prim】
- HDOJ 1233 还是畅通工程(并查集)
- 还是畅通工程hdoj1233
- hdoj-1233-还是畅通工程
- HDOJ 1233 还是畅通工程 【最小生成树】+【prim】
- 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】还是畅通工程