HDU 1233 还是畅通工程
2014-08-28 10:24
302 查看
还是畅通工程
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1233
64-bit integer IO format: %I64d Java class name: Main
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
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
Hint
Huge input, scanf is recommended.
Source
浙大计算机研究生复试上机考试-2006年解题:最小生成树
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; struct arc{ int u,v,w; arc(int x = 0,int y = 0,int z = 0){ u = x; v = y; w = z; } }; arc e[10000]; int n,m,uf[110]; bool cmp(const arc &x,const arc &y){ return x.w < y.w; } int Find(int x){ if(x != uf[x]) uf[x] = Find(uf[x]); return uf[x]; } int Kruskal(){ for(int i = 0; i <= n; i++) uf[i] = i; sort(e,e+m,cmp); int ans = 0; for(int i = 0; i < m; i++){ int x = Find(e[i].u); int y = Find(e[i].v); if(x != y){ uf[x] = y; ans += e[i].w; } } return ans; } int main() { int i,u,v,w; while(scanf("%d",&n),n){ m = n*(n-1)/2; for(i = 0; i < m; i++){ scanf("%d %d %d",&u,&v,&w); e[i] = arc(u,v,w); } printf("%d\n",Kruskal()); } return 0; }
View Code
相关文章推荐
- hdu 1233 还是畅通工程(最小生成树)
- HDU 1233 还是畅通工程 并查集 (单向点连通)
- 还是畅通工程(最小生成树)【HDU】-1233
- HDU 1233 还是畅通工程
- [HDU 1233] 还是畅通工程(最小生成树)
- HDU 1233 还是畅通工程(最小生成树)
- HDU 1233 还是畅通工程 并查集 (单向点连通)
- hdu 1233 还是畅通工程(最小生成树)
- hdu - 1233 - 还是畅通工程
- HDU-1233 还是畅通工程 (prim 算法求最小生成树)
- HDU 1233 还是畅通工程(模板——克鲁斯卡尔算法)
- HDU ~ 1233 ~ 还是畅通工程 (最小生成树)
- HDU 1233 还是畅通工程
- HDU 1233 还是畅通工程(最小生成树入门)
- HDU 1233 还是畅通工程 并查集 (单向点连通)
- HDU - 1233 还是畅通工程(最小生成树模板题)
- HDU1233----还是畅通工程
- hdu 1233 还是畅通工程
- hdu 1233 还是畅通工程
- HDU 1233 还是畅通工程(最小生成树)