POJ2349 ZOJ1914 UVA10369 Arctic Network【Kruskal算法+并查集】

509 篇文章 9 订阅
232 篇文章 0 订阅

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25651 Accepted: 7884

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


问题链接POJ2349 ZOJ1914 UVA10369 Arctic Network

问题描述:(略)

问题分析

  这是一个最小生成树的为问题,解决的算法有Kruskal(克鲁斯卡尔)算法和Prim(普里姆) 算法。

程序说明

  本程序使用Kruskal算法实现。有关最小生成树的问题,使用克鲁斯卡尔算法更具有优势,只需要对所有的边进行排序后处理一遍即可。程序中使用了并查集,用来判定加入一条边后会不会产生循环。程序中,图采用边列表的方式存储,排序一下就好了。

  这个问题求的是第s长的边,计算上要做适当处理。

  使用函数名distance()的话,在POJ中会产生编译错误,非常郁闷。


AC的C++语言程序如下:

/* POJ2349 ZOJ1914 UVA10369 Arctic Network */

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;

const int N = 500;

int f[N + 1], fcnt;

void UFInit(int n)
{
    for(int i = 1; i <=n; i++)
        f[i] = i;
    fcnt = n;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

bool Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
        fcnt--;
        return true;
    } else
        return false;
}

struct Point {
    int x, y;
} point[N + 1];

struct Edge {
    int src, dest;
    double cost;
} edges[N * (N - 1)];
double dist[N + 1];

bool cmp(Edge a, Edge b)
{
    return a.cost < b.cost;
}

inline double caldistance(Point a, Point b)
{
    return sqrt((double)(a.x - b.x) * (a.x - b.x) + (double)(a.y - b.y) * (a.y - b.y));
}

bool cmp2(double a, double b)
{
    return a > b;
}

int main()
{
    int n, s, p, cnt2;
    scanf("%d", &n);
    while(n--) {
        scanf("%d%d", &s, &p);

        UFInit(p);

        for(int i = 1; i <= p; i++)
            scanf("%d%d", &point[i].x, &point[i].y);

        cnt2 = 0;
        for(int i = 1; i <= p; i++)
            for(int j = i + 1; j <= p; j++) {
                edges[cnt2].src = i;
                edges[cnt2].dest = j;
                edges[cnt2++].cost = caldistance(point[i], point[j]);
            }

        // Kruscal算法
        sort(edges, edges + cnt2, cmp);
        int cnt3 = 0;
        for(int i = 0; i < cnt2; i++) {
            if(Union(edges[i].src, edges[i].dest)) {
                dist[++cnt3] = edges[i].cost;
                if(cnt3 == p - 1)
                    break;
            }
        }

        sort(dist + 1, dist + 1 + cnt3, cmp2);
        printf("%.2f\n", dist[s]);
    }

    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值