UVa 10397:Connect the Campus (最小生成树)2014-10-24 shuangde800 链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1338题目:Problem E
Connect the Campus
Input: standard input
Output: standard output
Time Limit: 2 secondsMany 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 CampusInputThe input file describes several test case. The description of each test case is given below:本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/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 Nlines give the x and 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 byM 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.OutputFor 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 Input4
103 104
104 100
104 103
100 100
1
4 24
103 104104 100104 103100 10014 2Sample Output
4.41
4.41分析与总结:最小生成树, 但是有点变化,有些边是已经建好的,那么把这些建好的边权值赋值为0,然后直接模板代码:1.Prim
#include<cstdio>#include<cstring>#include<cmath>#define N 760double w[N][N],x[N],y[N],key[N];int pre[N], hash[N], n, m;inline double getDist(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}double Prim(){memset(hash, 0, sizeof(hash));hash[1] = 1;for(int i=1; i<=n; ++i){key[i] = w[1][i]; pre[i]=1;}double sum=0;for(int i=1; i<n; ++i){int u=-1;for(int j=1; j<=n; ++j)if(!hash[j]){if(u==-1||key[j]<key[u])u=j;}sum += w[pre[u]][u];hash[u] = 1;for(int j=1; j<=n; ++j)if(!hash[j]){if(key[j]>w[u][j]){key[j] = w[u][j];pre[j] = u;}}}return sum;}int main(){int u,v;while(~scanf("%d",&n)){memset(w, 0, sizeof(w));for(int i=1; i<=n; ++i)scanf("%lf%lf",&x[i],&y[i]);for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j)if(i!=j)w[i][j] = getDist(x[i],y[i],x[j],y[j]);scanf("%d",&m);for(int i=0; i<m; ++i){scanf("%d%d",&u,&v);w[u][v]=w[v][u]=0;}printf("%.2f
", Prim());}return 0;}