(POJ

来源:互联网 发布:李智勇软件行业协会 编辑:程序博客网 时间:2024/06/10 07:20

(POJ - 2349)Arctic Network

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 22501 Accepted: 6951

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

题目大意:有p个前哨站,前哨站之间可以通过卫星或者无线电连通,有s个卫星无视距离(即无论什么距离都可以连通),无线电连通距离不超过D(D取决于需要连通的最远的两个前哨站之间的距离),问D最小是多少。

思路:先根据距离生成最小树,而距离远的用s个卫星连通,剩下的用无线电,显然是p-s大的边权(距离)就是我们要求的D。

#include<cstdio>#include<algorithm>#include<cmath>using namespace std; const int maxn=505;int fa[maxn];struct outpost{    double x,y;}a[maxn];struct node{    int u,v;    double w;}edge[maxn*maxn];bool cmp(node a,node b){    return a.w<b.w;}int find(int x){    if(x==fa[x]) return x;    else return fa[x]=find(fa[x]);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int s,p;        scanf("%d%d",&s,&p);        for(int i=0;i<p;i++)         {            fa[i]=i;            scanf("%lf%lf",&a[i].x,&a[i].y);        }        int tot=0;        for(int i=0;i<p-1;i++)            for(int j=i+1;j<p;j++)            {                double dx=a[i].x-a[j].x,dy=a[i].y-a[j].y;                double w=sqrt(dx*dx+dy*dy);                edge[tot++]=(node){i,j,w};            }        sort(edge,edge+tot,cmp);        int cnt=0;        double ans;        for(int i=0;i<tot;i++)        {            int fu=find(edge[i].u),fv=find(edge[i].v);            if(fu!=fv)            {                fa[fu]=fv;                cnt++;            }            if(cnt==p-s)            {                ans=edge[i].w;                break;            }        }        printf("%.2f\n",ans);    }    return 0;}
原创粉丝点击