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

从最小生成树中删除几条最长的边
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define INF 0x3f3f3f3f
#define MAXN 509

/*
最小生成树,把所有匹配的边记录下来处理
*/

struct node
{
    double x, y;
}a[MAXN];
double dist(const node& a, const node& b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
struct edge
{
    edge(int _a, int _b, double _c) :u(_a), v(_b), cost(_c) {}
    int u, v;
    double cost;
    bool operator<(const edge& rhs)const
    {
        return cost < rhs.cost;
    }
};
int pre[MAXN];
int T, k, n;
vector<edge> E;
vector<double> ans;
int find(int x)
{
    if (pre[x] == -1)
        return x;
    else
        return pre[x] = find(pre[x]);
}void mix(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (fx != fy)
        pre[fx] = fy;
}
void Kruskal()
{
    sort(E.begin(), E.end());
    for (int i = 0; i < E.size(); i++)
    {
        int f = E[i].u, t = E[i].v;
        if (find(f) != find(t))
        {
            mix(f, t);
            ans.push_back(E[i].cost);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> T;
    while (T--)
    {
        ans.clear();
        E.clear();
        memset(pre, -1, sizeof(pre));
        cin >> k >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i].x >> a[i].y;
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                E.push_back(edge(i, j, dist(a[i], a[j])));
            }
        }
        Kruskal();
        sort(ans.begin(), ans.end());
        k--;
        while (k)
            k--,ans.pop_back();
        cout << setiosflags(ios::fixed); 
        cout << setprecision(2) << ans.back() << endl;    //输出0位小数,3
    }
}

 

posted @ 2017-08-21 12:58  joeylee97  阅读(138)  评论(0编辑  收藏  举报