1. 程式人生 > >poj 2349Arctic Network(最小生成樹,按序加邊一些邊已聯通)

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