Connect the Campus
Input: standard input
Output: standard output
Time Limit: 2 seconds
Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables.
We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings).
You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.
Fig: University of Waterloo Campus
Input
The input file describes several test case. The description of each test case is given below:
The first line of each test case contains the number of buildings N (1<=N<=750). The buildings are labeled from 1 to N. The next N lines give the xand y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 <= M <= 1000) followed by M lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.
Output
For each set of input, output in a single line the total length of the new cables that you plan to use, rounded to two decimal places.
Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
4.41
题解:坐标存到数组里,把已经建好线路的距离权值赋初值为0,就不影响最后结果了。
#include <stdio.h>
#include <string.h>
#include <math.h>
const int N = 755;
const int INF = 1000000;
const int M = 300000;
int x[N], y[N], n;
double g[N][N];
double prim(){
double ans = 0;
double d[N];
int vis[N];
int index;
memset(vis, 0, sizeof(vis));
vis[1] = 1;
for(int i = 1; i <= n; i++)
d[i] = g[1][i];
for(int j = 1; j < n; j++){
double mincost = INF;
for(int k = 1; k <= n; k++){
if(!vis[k] && d[k] < mincost){
mincost = d[k];
index = k;
}
}
ans += mincost;
vis[index] = 1;
for(int q = 1; q <= n; q++)
if(d[q] > g[index][q])
d[q] = g[index][q];
}
return ans;
}
int main(){
int m, a, b, i, j;
while(scanf("%d",&n) != EOF){
memset(g, INF, sizeof(g));
for(i = 1; i <= n; i++)
scanf("%d%d", &x[i], &y[i]);
for(i = 1; i <= n; i++)
for(j = i + 1; j <= n; j++){
g[i][j] = sqrt((x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]));
g[j][i] = g[i][j];
}
scanf("%d",&m);
for(i = 1; i <= m; i++){
scanf("%d%d", &a, &b);
g[b][a] = g[a][b] = 0;
}
printf("%.2lf\n", prim());
}
return 0;
}