HDU1233--还是畅通工程(最小生成树)
2016-12-29 23:24
441 查看
Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。 Input 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。 Output 对每个测试用例,在1行里输出最小的公路总长度。 Sample Input
Sample Output
Source
浙大计算机研究生复试上机考试-2006年
Recommend
JGShining
还是最小生成树的裸题
kruskal算法
当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 Hint Huge input, scanf is recommended.
Source
浙大计算机研究生复试上机考试-2006年
Recommend
JGShining
还是最小生成树的裸题
kruskal算法
#include<iostream> #include<algorithm> #include<stdio.h> #define MAXN 10000 using namespace std; int s[MAXN]; int n,m; struct edge { int x; int y; int v; }G[MAXN]; int f(int x) { if(x==s[x]) return x; s[x]=f(s[x]); return s[x]; } int merg(int a,int b) { int x=f(a),y=f(b); if(x!=y){ s[x]=y; return 0; } return 1; } void init() { for(int i=0;i<=n;i++){ s[i]=i; } } bool cmp(struct edge a,struct edge b) { return a.v<b.v; } int main() { // #ifndef ONLINE_JUDGE // freopen("data.in", "r", stdin); // #endif int a,b; int res; int round; while(scanf("%d",&n),n){ for(int i=0;i<=n;i++){ s[i]=i; } res=0; round=n*(n-1)/2; for(int i=0;i<round;i++){ scanf("%d%d%d",&G[i].x,&G[i].y,&G[i].v); } sort(G,G+round,cmp); for(int i=0;i<round;i++){ a=f(G[i].x); b=f(G[i].y); if(a!=b){ s[a]=b; res+=G[i].v; } } // cout<<res<<endl; printf("%d\n",res); } return 0; }
相关文章推荐
- hdu 1233 还是畅通工程(最小生成树)
- HDU 1233 还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树Kruskal)
- poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)
- HDU-1233 还是畅通工程 (prim 算法求最小生成树)
- HDU - 1233 - 还是畅通工程 (最小生成树!!)
- HDU 1233 还是畅通工程(最小生成树,prim)
- HDU 1233 还是畅通工程(最小生成树Kruskal)
- HDU 1233 还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树)
- hdu 1233 最小生成树 "还是畅通工程"
- HDU 1233 还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树prim)
- [HDU 1233] 还是畅通工程(最小生成树)
- HDU 1233-还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树的Prim和Kruskal两种算法的c++实现)(prim算法详解)
- hdu-1233 还是畅通工程(最小生成树)
- hdu 1233 还是畅通工程(最小生成树,基础)
- HDU - 1233 还是畅通工程(最小生成树模板题)