UVa 10397 Connect the Campus

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 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 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 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


(Problem-setters: G. Kemkes & G. V. Cormack, CS Dept, University of Waterloo)

 

“A man running away from a tiger need not run faster than the tiger but run faster than the friend.”

 

给一个无向非连通图,在图上加一些边使整个图连通,求加的边总长最少为多少

最小生成树问题,可以使用Kruskal算法,只不过在运行算法之前先将图中已经连通的点连在一起,再继续Kruskal

 

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<cmath>
  5 #include<vector>
  6 
  7 using namespace std;
  8 
  9 typedef struct
 10 {
 11     int x;
 12     int y;
 13 } POINT;
 14 
 15 typedef struct
 16 {
 17     int s;
 18     int e;
 19     double dis;
 20 } CABLE;
 21 
 22 struct cmp
 23 {
 24     bool operator()(CABLE a,CABLE b)
 25     {
 26         return a.dis>b.dis;
 27     }
 28 };
 29 
 30 int V,E;
 31 int par[800];
 32 POINT p[800];
 33 double G[800][800];
 34 
 35 double Distance(POINT a,POINT b)
 36 {
 37     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 38 }
 39 
 40 void Initial()
 41 {
 42     for(int i=1;i<=V;i++)
 43         par[i]=i;
 44 }
 45 
 46 int Find(int x)
 47 {
 48     if(par[x]==x)
 49         return x;
 50     return par[x]=Find(par[x]);
 51 }
 52 
 53 bool Unite(int x,int y)
 54 {
 55     int par_x=Find(x);
 56     int par_y=Find(y);
 57     if(par_x!=par_y)
 58     {
 59         par[par_y]=par_x;
 60         return true;
 61     }
 62     return false;
 63 }
 64 
 65 int main()
 66 {
 67     while(scanf("%d",&V)==1)
 68     {
 69         for(int i=1;i<=V;i++)
 70             scanf("%d %d",&p[i].x,&p[i].y);
 71 
 72         priority_queue<CABLE,vector<CABLE>,cmp> q;
 73 
 74         for(int i=1;i<=V;i++)
 75             for(int j=i+1;j<=V;j++)
 76             {
 77                 G[i][j]=Distance(p[i],p[j]);
 78                 CABLE temp;
 79                 temp.s=i;
 80                 temp.e=j;
 81                 temp.dis=G[i][j];
 82                 q.push(temp);
 83             }
 84 
 85         scanf("%d",&E);
 86         Initial();
 87 
 88         int total=0;
 89         for(int i=1;i<=E;i++)
 90         {
 91             int a,b;
 92             scanf("%d %d",&a,&b);
 93             if(Unite(a,b))
 94                 total++;
 95         }
 96 
 97         double ans=0;
 98         while(total<V-1)
 99         {
100             CABLE temp=q.top();
101             q.pop();
102             if(Unite(temp.s,temp.e))
103             {
104                 ans+=temp.dis;
105                 total++;
106             }
107         }
108 
109         printf("%.2lf\n",ans);
110     }
111 
112     return 0;
113 }
[C++]

 

posted @ 2014-02-27 15:06  ~~Snail~~  阅读(350)  评论(0编辑  收藏  举报