POJ 2349-Arctic Network【最小生成树】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/include_peng/article/details/86651714

题目描述

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

题意

需要自己计算边权的最小生成树,然后选需要建立的第k小边。

写这一篇的原因:

  1. 题意是选择第p-s个边,需要读懂才能选对。
  2. 写完一直是Output Limit Exceeded,但是并没有发现多输出了什么,后来发现了两个错误,一个坐标应用double类型而不是int,一个是存边的结构体数组粗心少开了100倍,以此铭记!!
  3. 很久没更博了~

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

const int Maxe = 250000;
const int Maxn = 505;

struct edge{
    int from,to;
    double val;
};
struct point{
    double x,y;
};
point pt[Maxn];
edge e[Maxe];

int par[Maxn];
int cnt,t,s,p;

bool cmp(edge e1, edge e2)
{
    return e1.val<e2.val;
}
void Init()
{
    for(int i = 0; i < Maxn; i++)
        par[i] = i;
}
int Find(int x)
{
    if(x==par[x])
        return par[x];
    else
        return par[x] = Find(par[x]);
}
bool Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x!=y)
    {
        par[y] = x;
        return true;
    }
    else
        return false;
}
double Kruskal()
{
    int res = 0;
    sort(e,e+cnt,cmp);
    Init();
    for(int i = 0; i < cnt; i++)
    {
        if(Union(e[i].from, e[i].to))
        {
            res++;
            if(res == p-s)
                return e[i].val;
        }
    }
}


int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &s, &p);
        cnt = 0;
        for(int i = 0; i < p; i++)
            scanf("%lf %lf", &pt[i].x, &pt[i].y);
        for(int i = 0; i < p-1; i++)
        {
            for(int j = i+1; j < p; j++)
            {
                e[cnt].from = i;
                e[cnt].to = j;
                e[cnt++].val = (double)sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y));
            }
        }
        printf("%.2f\n", Kruskal());
    }
    return 0;
}

如有错误请指明~   ฅ●ω●ฅ

猜你喜欢

转载自blog.csdn.net/include_peng/article/details/86651714