Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17363 Accepted Submission(s): 6606
Problem 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.
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.
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库里斯卡尔算法 # include<stdio.h> # include<string.h> # include<algorithm> # define max 100000 + 100 using namespace std; int z, a, b; int m, n; int per[max]; struct node { int x; int y; int w; }c[max]; void g() { for(int i = 0; i <= z; i++) per[i] = i; } int cmp(node x, node y) { return x.w < y.w; } int find(int x) { if(x == per[x]) return x; return per[x] = find(per[x]); } int hebing(int x, int y) { int fx = find(x); int fy = find(y); if(fx != fy) { per[fx] = fy; return true; } return false; } int main() { while(scanf("%d",&z) != EOF) { g(); int k = 0; for(int i = 1; i <= z; i++) //i,j必须从一开始 ,因为i,j表示城镇的标号。 { for(int j = 1; j <= z; j++) { scanf("%d",&a); if(j > i) { c[k].x = i; c[k].y = j; c[k].w = a; k++; } } } sort(c, c + k, cmp); scanf("%d", &b); for(int i = 0; i < b; i++) { scanf("%d%d",&m,&n); hebing(m,n); } int sum = 0; for(int i = 0; i < k; i++) { if(hebing(c[i].x,c[i].y)) { sum = sum + c[i].w; } } printf("%d\n",sum); } return 0; } 普利姆算法 # include<cstdio> # include<cstring> # include<cmath> # define MAX 0x3f3f3f3f # define max 100 + 10 /*tree[]用来标记点是否经过 map[]用来保存两村庄之间的距离 low[]是保存起点到其余所有点的距离 */ int z, n, a, b, len, point; int tree[max], map[max][max], next; int low[max]; void prim() { int i, j, next, min, mindis; memset(tree, 0 ,sizeof(tree)); tree[1] = 1; mindis = 0; for(i = 1; i <= point; i++) { low[i] = map[1][i]; } for(j = 1; j < point; j++) { min = MAX; for(i = 1; i <= point; i++) { if(!tree[i] && low[i] < min) { min = low[i]; next = i; } } mindis = mindis + min; tree[next] = 1; for(i = 1; i <= point; i++) { if(!tree[i] && low[i] > map[next][i]) { low[i] = map[next][i]; } } } printf("%d\n",mindis); } int main() { while(scanf("%d",&z) != EOF) { point = z; memset(map,MAX,sizeof(map)); for(int i = 1; i <= z; i++) { for(int j = 1; j <= z; j++) { scanf("%d",&len); map[i][j] = map[j][i] = len; } } scanf("%d",&n); for(int i = 1; i <= n; i++) { scanf("%d%d",&a,&b); map[a][b] = map[b][a] = 0;//使两村庄之间距离为0,求要修最短路径时就不需要加上这条边了。 } prim(); } return 0; }