Arctic Network (poj 2349 最小生成树)


Language:
Default

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

Waterloo local 2002.09.28

题意:有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;
}

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

时间: 2024-11-03 05:33:42

Arctic Network (poj 2349 最小生成树)的相关文章

(最小生成树) Arctic Network -- POJ --2349

链接: http://poj.org/problem?id=2349 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 510; const int INF = 0xfffffff;

Arctic Network poj 2349

http://poj.org/problem?id=2349 题意:现有 m 个地点, n 个卫星.  卫星有一个神奇的作用, 作用在于只要某个地方安装有卫星, 不论他们之间距离为多远, 都能够互相接收到彼此间信 号, (这么好的东西当然不会让每个地方都有的), 那么其他地方需要通过收发器 D 来连接.让你求1 - m 个地方在共安装过 n 个卫星后,我们所需要     的 收发器 D 所需的最大距离是多少?   *****一开始提交错误仍然是因为%lf和%f,吸取吸取教训... *******

POJ2349:Arctic Network(二分+最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题目链接:http://poj.org/problem?id=2349 Description: The Department of National Defence (DND) wishes to connect several northern outposts by a wireless net

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network 题目传送 Sol: 方法一: 贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案. code: #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define IL inline #define RG register

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

[Poj2349]Arctic Network(二分,最小生成树)

[Poj2349]Arctic Network Description 国防部(DND)要用无线网络连接北部几个哨所.两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道. 任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置.同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率.功率越大,D也越大,但成本更高.出于采购和维修的方便,所有哨所的收发器必须是相同的:那就是说,D值对每一个哨所相同. 你的任务是确定收发器的D的最