POJ2421 Constructing Roads
来源:互联网 发布:unity3d 手游开发 编辑:程序博客网 时间:2024/06/05 01:13
Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
题目传送门
求1~n的最短路(其中有些边已经建好,不用代价)
和上一题POJ2485没什么区别
就这样吧,新建一条权值为0的边就好了。
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;struct node{ int x,y,d; node() { x=0; y=0; d=0; }} a[1100000];int len;int cmp(const void*x1,const void*x2){ node n1=*(node*)x1; node n2=*(node*)x2; return n1.d-n2.d;}int fa[1100000];int findfa(int x){ if(fa[x]!=x)return findfa(fa[x]); return x;}int main(){ int n,T; while(scanf("%d",&n)!=EOF) { len=0; int d; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { scanf("%d",&d); if(d!=0) { len++; a[len].x=i,a[len].y=j,a[len].d=d; } } int m; scanf("%d",&m); while(m--) { int x,y; scanf("%d%d",&x,&y); len++; a[len].x=x;a[len].y=y;a[len].d=0; } for(int i=1;i<=n;i++)fa[i]=i; qsort(a+1,len,sizeof(node),cmp); int t=0; int ans=0; for(int i=1; i<=len; i++) { int x=a[i].x,y=a[i].y; int fx=findfa(x),fy=findfa(y); if(fx!=fy) { fa[fy]=fx; t++; ans+=a[i].d; } if(t==n-1) { break; } } printf("%d\n",ans); } return 0;}
by_lmy
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- POJ2421 Constructing Roads
- HDU1102&&POJ2421 Constructing Roads
- poj2421 [java]Constructing Roads
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- poj2421——Constructing Roads
- poj2421 Constructing Roads (prime)
- 2011.10.28 poj2421 Constructing Roads 解题报告
- POJ2421 Constructing Roads [最小生成树 prim]
- poj2421 Constructing Roads【最小生成树】
- POJ2421 Constructing Roads(最小生成树)
- POJ2421 Constructing Roads(最小生成树,Prim)
- Agri Net POJ1258 && Constructing Roads POJ2421
- Constructing Roads*
- NYOJ995
- 前端面试拾遗——CSS选择器、Flex布局、position
- azure 最佳实践-- 为演化而设计
- 淘宝放大镜
- 2017 ACM/ICPC Asia Regional Qingdao Online--Chinese Zodiac
- POJ2421 Constructing Roads
- ActiveMQ开篇学习笔记
- C语言中的内存分配
- 周末训练笔记(三)
- 32位JVM对long类型的赋值不是原子性操作
- azure 最佳实践 -- 随业务演化的架构
- 计算机原码、反码、补码详解
- 单例模式的写法
- Mac下的myeclipse安装