Arctic Network POJ - 2349 (最小生成树Kruskal)

本文介绍了一种使用最小生成树算法解决特定无线网络连接问题的方法。该问题涉及确定最少数量的高功率无线电设备,以确保所有偏远站点能够通过直接或间接方式互相通信。文章详细说明了算法实现步骤,并提供了完整的代码示例。

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

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个站点,s个卫星系统,每个卫星系统只能安排在一个站点 ,然后有卫星系统的站点间通讯不需要代价,否则要连接两个站点所花代价为两个站点间的距离。问使得所有站点连通所花费的代价中最大的代价最小是多少。

思路:首先肯定是生成一颗最小生成树,然后将s个卫星放置在前s个距离最大的点上,那么剩下的最大距离即是最小的最大花费。
然后巨坑的是 poj输出 double 也用%f 不然一般会wa ????

代码如下

#include <iostream> 
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std; 
#define ll long long int 

int pre[505];
int tol;
int n;
struct NODE//链式向前星存边
{
    int u,v;
    double cost;
}edge[505*505];

void add(int u,int v,double cost)
{
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol].cost=cost;
    tol++;
}
int find(int x)
{
   int r=x;
   while (pre[r]!=r)
   r=pre[r];
   int i=x; int j;
   while(i!=r)
   {
       j=pre[i];
       pre[i]=r;
       i=j;
   }
   return r;
}
int join(int a,int b)
{
    int f1=find(a);
    int f2=find(b);
    if(f1==f2) return 0;
    else 
    {
        pre[f2]=f1;
        return 1;
    }
}

void init()
{
    for(int i=1;i<=n;i++)
    pre[i]=i;
    tol=0;
}

bool cmp(NODE a,NODE b)
{
    return a.cost<b.cost;
}

struct node 
{
    int x,y;
};
node dian[505];

double len[505];
int main() 
{  
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int s;
        scanf("%d%d",&s,&n);

        for(int i=1;i<=n;i++)
        scanf("%d%d",&dian[i].x,&dian[i].y);
        init();
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
                double cost;
                cost=sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y));//计算两点间的距离
                add(i,j,cost);
        }

        sort(edge,edge+tol,cmp);
        int flag=n;
        for(int i=0;i<tol;i++)
        {
            if(join(edge[i].u,edge[i].v))
            {
                flag--;
                if(flag==s)//剩下的s个站点都可以用卫星来代替通讯,所以此时的距离为最大距离
                {
                    printf("%.2f\n",edge[i].cost);
                    break;
                }
            }
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值