(POJ - 2349)Arctic Network

本文解析了POJ-2349北极网络问题,介绍了如何通过生成最小生成树来确定使所有前哨站通过卫星或无线电连接所需的最小无线电传输距离D。文章提供了完整的C++代码实现,利用Kruskal算法解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值