poj-2349--Arctic Network

Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:24462 Accepted: 7539

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

题目大意:

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

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

思路:

很自然的想到求最小生成树,然后为了使得D费用最少,就要让其中路径最长的那些用来安装卫星。 s个通信卫星可以安装s-1条最长的那些路径。 那么, 最小生成树中第p-s大的路径长度就是D。

只需要求出最小生成树上的所有路径长度,排好序,即可得到答案。

代码:

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct ss
{
    int u,v;
    double w;
} a[1010001];
struct sss
{
    int x,y;
} b[101001];
int fu[101001];
int findroot(int x)
{
    if(fu[x]!=x)
        fu[x]=findroot(fu[x]);
    return fu[x];
}
int cmp(ss x,ss y)
{
    return x.w<y.w;
}
int hebing(int a,int b)
{
    int x,y;
    x=findroot(a);
    y=findroot(b);
    if(x==y)
        return 0;
    fu[y]=x;
    return 1;
}
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        int m,n,i,j,k=0,ans1=0,x[101001],y[101011];
        double sum=0;
        scanf("%d%d",&m,&n);
        for(i=0; i<=n; i++)
            fu[i]=i;
        for(i=1; i<=n; i++)
            scanf("%d%d",&x[i],&y[i]);
        for(i=1; i<=n; i++)
        {
            for(j=i+1; j<=n; j++)
            {
                a[k].u=i;
                a[k].v=j;
                a[k].w=sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
                k++;
            }
        }
        sort(a,a+k,cmp);
        for(i=0; i<k; i++)
        {
            if(hebing(a[i].u,a[i].v))
                ans1++;
            if(ans1==n-m)
            {
                sum=a[i].w;
                break;

            }
        }
        printf("%.2lf\n",sum);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值