友谊赛 最小生成树

 

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

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<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 1000
using namespace std;
double w[N][N],x[N],y[N];
int per[80010];
int n;
struct node{
    int u,v;
    double w;
    }a[800010];
double cmp(node a,node b)
{
	 return a.w<b.w;
}

 double  dis(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

void init(){
    for(int i=0; i<=n*n; ++i)
       per[i]=i;
}
int find(int x)
{    
    int r=x;
    while(r!=per[r])
      r=per[r];
    int i=x,j;
    while(i!=r)
     {
     	 j=per[i];
     	 per[i]=j;
     	  i=j;
     }
     return r;
}
bool join(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
     {per[fx]=fy;
      return true;
     }
     return false;
}
int main(){
    int m,u,v;
    while(~scanf("%d",&n)){
        for(int i=1; i<=n; ++i)
            scanf("%lf%lf",&x[i],&y[i]);
        for(int i=1; i<=n; ++i)
            for(int j=i+1; j<=n; ++j)
                w[i][j] = dis(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;
        }
        int pos=0;
        for(int i=1; i<=n; ++i)
            for(int j=i+1; j<=n; ++j){
                a[pos].u=i,a[pos].v=j;
                a[pos++].w=w[i][j];
            }
        init();
        sort(a,a+pos,cmp);
        double ans=0;
        for(int i=0; i<pos; ++i){
            if(join(a[i].u,a[i].v))
                ans += a[i].w;
        }
        printf("%.2f\n", ans);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值