G - Arctic Network POJ - 2349

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个卫星,有卫星的2点可以直接相连,不需要连线。求需要的单个最大边最小是多少。首先肯定是求最小生成树,将生成树上的每个边保存。然后要从大到小分配卫星,使较大的边权为0。那么s个卫星可以使s-1条大的边为0,那么就是求最小生成树上第k大的边了。

这个博客有详细的证明:

http://blog.csdn.net/mengxiang000000/article/details/51482790
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
const int maxn = 1010;
int vis[maxn],s,p,tot;
double g[maxn][maxn],d[maxn],edg[maxn];

struct node
{
    double x,y;
}nod[maxn];

double len(int i,int j)
{
    return sqrt((nod[i].x - nod[j].x) * (nod[i].x - nod[j].x) + (nod[i].y - nod[j].y) * (nod[i].y - nod[j].y));
}

void prim()
{
    memset(vis,0,sizeof(vis));
    for(int i = 1;i <= p;i++) d[i] = 10010;
    d[1] = 0;
    tot = -1;

    while(1)
    {
        int v = -1;
        for(int u = 1;u <= p;u++)
        {
            if(!vis[u] && (v == -1 || d[v] > d[u])) v = u;
        }
        if(v == -1) break;
        vis[v] = 1;
        edg[++tot] = d[v];
        for(int u = 1;u <= p;u++)
        {
            if(!vis[u]) d[u] = min(d[u],g[v][u]);
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&s,&p);
        for(int i = 1;i <= p;i++)
            for(int j = 1;j <= p;j++) g[i][j] = g[j][i] = 10010;

        for(int i = 1;i <= p;i++)
        {
            scanf("%lf %lf",&nod[i].x,&nod[i].y);
        }
        for(int i = 1;i <= p;i++)
        {
            for(int j = i + 1;j <= p;j++)
            {
                g[i][j] = g[j][i] = len(i,j);
            }
        }

        prim();
        sort(edg + 1,edg + 1 + tot);
        printf("%.2lf\n",edg[tot - s + 1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Eric_chen_song_lin/article/details/82758544
今日推荐