Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1338
Topic:
Problem E
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 are have been hired to ensure this each building are connected to every other building (directly or indirectly) Throug H The campus network of communication cables.
We'll treat each building as a point specified by a 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, but they are only connected together at their endpoints (at buildings).
You have been given a campus map which shows the locations to all buildings and existing communication. You are must not alter the existing cables. Determine where to install the new communication cables so and all buildings are. Of course, the university wants you to minimize the amount of new cable.
Fig:university of Waterloo Campus
Input
The input file describes several test case. The description of each test case is given below:
This column more highlights: http://www.bianceng.cn/Programming/sjjg/
The ' a ' 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 and absolute values at most 10000. No two buildings occupy the same point. After this there is a line containing the number of existing cables M (0 <= M <= 1000) followed Bym lines describing The existing cables. Each cable was represented by two integers:the building numbers which are directly connected by the cable. There is in most one cable directly connecting each pair of buildings.
Output
For each set of input, output ' a single ' total length of the ' new cables that ' you ' to ' use ', rounded to two Dec iMAL 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
Analysis and Summary:
Minimum spanning tree, but a little bit of change, some edges are already built, then assign these Benquan values to 0 and then directly template
Code:
1.Prim
#include <cstdio> #include <cstring> #include <cmath> #define N 760 Double 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\n", Prim ());
return 0; }