还是畅通工程
2015-08-28 21:01
176 查看
L - 还是畅通工程
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
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
模板题
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
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
模板题
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; const int N=11115; int pa ,n; struct node { int u,v,w; bool operator<(const node &C)const { return w<C.w; } } t ; int cha(int x) { if(x!=pa[x]) { pa[x]=cha(pa[x]); } return pa[x]; } int cl() { int sum=0,top=0; for(int i=1; i<=(n*(n-1))/2; i++) { int x=t[i].u; int y=t[i].v; x=cha(x); y=cha(y); if(x!=y) { pa[x]=y; sum+=t[i].w; top++; } } return sum; } int main() { while(scanf("%d",&n)!=EOF&&n) { for(int j=1; j<=n; j++) { pa[j]=j; } for(int i=1; i<=(n*(n-1))/2; i++) { scanf("%d%d%d",&t[i].u,&t[i].v,&t[i].w); } sort(t+1,t+((n*(n-1))/2)+1); printf("%d\n",cl()); } return 0; }
相关文章推荐
- 机试——华为
- 霍夫曼树 java实现
- 强大而又简洁的微云
- 阻止外部滚动条
- 数组与指针
- 利用fastjson反序列化json为对象和对象数组
- 手机H5 web调试利器——WEINRE (WEb INspector REmote)
- Hadoop概括——学习笔记<一>
- XCode Could not launch "" failed to get the task for process
- 08-27 GridView(点中后,图片被蒙住<蒙板>)、Toast(点击后显示提示的内容,一会儿自动消失)、Dialog(点击后显示对话框)
- fdisk命令
- Vector,ArrayList, LinkedList
- 事件分发、拦截、消费(一)
- 并查集专题-1001 Freckles
- python 深度模仿 matlab 矩阵语法
- Mac上安装 Maven
- 剑指offer-第五章优化时间和空间效率(从1到n的整数中1出现的次数)
- 待熟悉的工具或API清单列表
- [转]Spring Security学习总结二
- Foundation框架中字符串的查找(rangOfString)