uva 10369

来源:互联网 发布:c语言 while break 编辑:程序博客网 时间:2024/06/06 15:40

Arctic Network(求最小生成树的第k小边)

Problem C: Arctic Network

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel. 

Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000). For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

1

2 4

0 100

0 300

0 600

150 750

Sample Output

212.13

题目大意:

南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连。任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远。 而安装有无线电设备的两个站,距离不能超过D。 D越长费用越多。

现在有s个卫星设备可以安装,还有足够多的无线电设备,求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径)。

 

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<iostream>

#include<cmath>

using namespace std;

#define INF 99999999

struct node

{

    int x;

    int y;

}e[505];

struct dian

{

    int st;

    int ed;

    double val;

}p[150005];

int s,t;

int pre[150005];

double juli(node a,node b)

{

    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

}//两点间距离

int cmp(dian a,dian b)

{

    return a.val<b.val;//按照价值从小到大排

}

void make()

{

    for(int i=0;i<150005;i++)

    {

        pre[i] = i;

    }

    return ;

}//把每个元素初始化

int Find(int m)

{

    int n=m;

    while(n!=pre[n])

        n=pre[n];

    //pre[m]=n;

    return n;

}//查找一个元素所在的集合,即找到根节点

void Merge(int x,int y)

{

    int fx=Find(x);

    int fy=Find(y);

    if(fx!=fy)

        pre[fx]=fy;

}//合并x,y

void kruskal(int l)//l为边的数量

{

    double sum=0;

    int time=t;

    make();

    for(int i=0;i<l;i++)

    {

        if(Find(p[i].st)!=Find(p[i].ed))//根节点不同

        {

            sum+=p[i].val;

            Merge(p[i].st,p[i].ed);//合并

            time--;

            if(time==s)

            {

                printf("%.2lf\n",p[i].val);

                return ;

            }

        }

    }

}

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d",&s,&t);

        for(int i=0;i<t;i++)

        {

            scanf("%d%d",&e[i].x,&e[i].y);

        }

        int k=0;

        for(int i=0;i<t;i++)

        {

            for(int j=i+1;j<t;j++)

            {

                p[k].st=i;

                p[k].ed=j;

                p[k].val=juli(e[i],e[j]);

                k++;

            }

        }

        sort(p,p+k,cmp);

        kruskal(k);

    }

    return 0;

}