1. 程式人生 > >Arctic Network (poj 2349 最小生成樹)

Arctic Network (poj 2349 最小生成樹)

Language: Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12397 Accepted: 4053

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



題意:有P個前哨,現在想把他們連成一個整體(也就是最小生成樹),有兩種方式可以連線(1)衛星連線,只要兩個前哨中有一個有衛星,他們就可以通訊(2)發射無線電,但是有一定的花費,與距離成正比。求最小的花費(即求無線電通訊的所有距離中的最大值)

思路:先用Kruskal求出最小生成樹,較長的邊使用衛星來通訊,那麼答案就是ans[P-1-S]。喔,這程式碼在poj上要用C++交才能過,不知道怎麼回事。。。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 555;
const int MAXN = 2005;
const int MAXM = 300010;
const int N = 1005;

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

struct Edge
{
    int u,v;
    double len;
}edge[MAXM];

double ans[maxn];
int S,P,cnt;
int father[maxn];

void init()
{
    cnt=0;
    for (int i=0;i<=P;i++)
        father[i]=i;
}

int cmp(Edge e1,Edge e2)
{
    return e1.len<e2.len;
}

double Dis(Node n1,Node n2)
{
    return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));
}

int find_father(int x)
{
    if (x!=father[x])
        father[x]=find_father(father[x]);
    return father[x];
}

double Kruskal()
{
    int i,j;
    sort(edge,edge+cnt,cmp);
    int num=0;
    for (i=0;i<cnt;i++)
    {
        int fu=find_father(edge[i].u);
        int fv=find_father(edge[i].v);
        if (fu!=fv)
        {
            father[fu]=fv;
            ans[num++]=edge[i].len;
            if (num==P-1) break;
        }
    }
    return ans[num-S];
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,t;
    sf(t);
    while (t--)
    {
        sff(S,P);
        init();
        for (i=1;i<=P;i++)
            scanf("%lf%lf",&node[i].x,&node[i].y);
        for (i=1;i<=P;i++)
        {
            for (j=i+1;j<=P;j++)
            {
                if (i==j) continue;
                edge[cnt].u=i;
                edge[cnt].v=j;
                edge[cnt++].len=Dis(node[i],node[j]);
            }
        }
        printf("%.2lf\n",Kruskal());
    }
    return 0;
}