poj 2349Arctic Network(最小生成树,按序加边一些边已联通)

Description

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.

Input

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

Output

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

Source

Waterloo local 2002.09.28

有p个站点,两种方式,一种通过卫星,任意两个都能连,卫星用工S个,另一种通过无线电,每一个站点都有无线电,只能连距离不超过D的,现在让你把所有联通,问你最小的D是多少

思路,按从小到大排序,用克鲁斯卡尔算法生成最小生成树,直到最后剩下S个分量,这时的距离就是要求的D,因为已经排好序了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 1010
#define maxm 5000005
struct point
{
    double x,y;
}p[maxn];
 int n,m;
struct edge
{
    int u,v;
    double dis;
    edge()
    {

    }
    edge(int u,int v,double d):u(u),v(v),dis(d){}
    bool operator<(const edge&rhs)const
    {
        return dis<rhs.dis;
    }
}edges[maxm];
int father[maxn];
int find(int x)

{
    if(father[x]!=x)
        father[x]=find(father[x]);
    return father[x];
}

double getdis(int i,int j)
{double x1=p[i].x;
double y1=p[i].y;
double x2=p[j].x;
double y2=p[j].y;
  return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

}
int main()
{
    int t;
    scanf("%d",&t);

    int cnt;
    while(t--)
    {
    cnt=0;
    scanf("%d%d",&m,&n);
    for(int i=0;i<=n;i++)
    father[i]=i;
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);

    int cnt=0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
        edges[cnt++]=edge(i,j,getdis(i,j));
    sort(edges,edges+cnt);
    int num=0;
    double d;
    for(int i=0;i<cnt;i++)
    {
        int x=edges[i].u;
        int y=edges[i].v;

        if(find(x)!=find(y))
        {
            father[find(x)]=find(y);

            if(++num>=n-m)
            {
                d=edges[i].dis;
                break;
            }

        }
    }
    printf("%0.2lf\n",d);


}
return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/84952140