首页 > Web开发 > 详细

Arctic Network (poj 2349 最小生成树)

时间:2015-07-27 19:01:17      阅读:168      评论:0      收藏:0      [点我收藏+]

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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Arctic Network (poj 2349 最小生成树)

原文:http://blog.csdn.net/u014422052/article/details/47087129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!