POJ 2349 Arctic Network

Arctic Network

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30989 Accepted: 9347

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

 

又是最小生成树,之前一直都是用Kruscal+并查集,这次用一下Prim,差点写成Dijstra。。。

之前题目有个看不懂的地方就是satellite channel的描述,题目的意思应该是每个点有一个satellite channel,而不是两个点之间。

题意+思路:一张图,有P个点,需要把他们连接起来(生成树),每个点有各自的坐标,现在给你S个卫星频道,可以分给S个点,那么这些点就可以白嫖进行通信,不需要任何代价,也就是生成树中的S-1条边是免费的。但是S不够P个点全部使用,其他的之间需要通过中继器进行通信,并且两个点之间的距离超过D就不能通信,求最小的D。使用最小生成树,对于其中的较大的S-1条边让他们白嫖即可,其他的则通过中继器,那么倒数第S条边就是答案。

 

#include <cstdio>
#include <cmath>
#include <string.h>
#include <algorithm>
const int MaxP = 505;
const double INF = 1e9;
bool used[MaxP];        // 标记该点是否已加入生成树中
double dis[MaxP], dist[MaxP][MaxP], edges[MaxP];
// dis[i]分别标记已在生成树中的点到未在生成树中的点i的最小距离, dist[u][v]代表u,v两点之间的距离,edges代表最终放入生成树中的点形成的边
struct point
{
    int x, y;
}points[MaxP];      // 记录点的坐标
int N, S, P;

int main()
{
    scanf("%d", &N);
    while(N--)
    {
        scanf("%d %d", &S, &P);
        for(int i = 0; i < P; i++)    scanf("%d %d", &points[i].x, &points[i].y);
        for(int i = 0; i < P; i++)      // 计算各个点之间的距离
        {
            for(int j = 0; j < P; j++)
            {
                double dx = points[i].x - points[j].x, dy = points[i].y - points[j].y;
                dist[i][j] = sqrt(dx * dx + dy * dy);
            }
        }
        memset(used, 0, sizeof(used));      // 初始化
        used[0] = true;                     // 假设以0为起点
        for(int i = 0; i < P; i++)          // 当前已在生成树中只有点0,计算其到其他点最短距离
        {
            dis[i] = dist[0][i];
        }
        int cnt = 0;                        // 记录当前生成树中边的数量
        for(int i = 1; i <= P - 1; i++)
        {
            int k = 0;
            double mind = INF;
            for(int j = 0; j < P; j++)      // 选择距离最小的点
            {
                if(!used[j] && dis[j] < mind)
                {
                    mind = dis[j];
                    k = j;
                }
            }
            used[k] = true;
            edges[cnt++] = mind;            // 加入该边
            for(int j = 0; j < P; j++)      // 根据刚加入的点更新最短距离信息
                if(!used[j] && dist[k][j] < dis[j])    dis[j] = dist[k][j];
        }
        std::sort(edges, edges + cnt);
        printf("%.2f\n", edges[cnt - S]);
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值