UVaOJ 10369 - Arctic Network


——by A Code Rabbit


Description

一个地区的几个城市要通讯。

通讯的设备强度,可以决定一个城市,可以向周围距离多少的城市广播。

因此,只需要几个城市连接上卫星,然后就可以向周围不停的广播。

而收到广播的城市可以继续向周围的城市广播,就可以让更多的城市通讯。

但是设备越牛逼,价格越高,因此只要距离刚刚好,就不需要买更牛逼的设备。

输入城市的坐标和有几个卫星。

输出设备应该达到的最短通讯距离。


Type

Graph Algorithms


Analysis

MST,Kruskal。

我们假设每个城市,都已经连上卫星。

之后我们的任务,就是把这些城市连接起来,减少卫星的数量直到符合题意。

我们想到Kruskal每连接一条边,就相当于把城市连接起来。

因此假设有n个城市,k个卫星。

我们就只需要连接n-k条边,就可以达到题目的要求。

并求由于Kruskal的边,是从小到大添加的。

因此只要记录下最后一条添加的边的权值,就是最短通讯范围。


Solution

// UVaOJ 10369
// Arctic Network
// by A Code Rabbit

#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

const int MAXV = 502;
const int MAXE = 502 * 502;

template <typename T>
struct Point {
    T x, y;
    static double GetDistance(Point a, Point b) {
        return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
    }
};

template <typename T>
struct Edge {
    int u, v;
    T w;
};

template <typename T>
struct Graph {
    Edge<T> edge[MAXE];
    int tot_edge;
    void Init() {
        tot_edge = 0;
    }
    void AddEdge(int u, int v, T w) {
        edge[tot_edge].u = u;
        edge[tot_edge].v = v;
        edge[tot_edge].w = w;
        tot_edge++;
    }
};

namespace Kruskal {
    int p[MAXV];
    template <typename T>
    int Cmp(Edge<T> a, Edge<T> b) { return a.w < b.w; }
    int Find(int x) { return p[x] == x ? x : p[x] = Find(p[x]); }
    template <typename T>
    T Go(Edge<T> e[MAXE], int n, int m, int s) {
        for (int i = 0; i < n; i++) p[i] = i;
        sort(e, e + m, Cmp<T>);
        T ans = 0;
        int cnt_edge = 0;
        for (int i = 0; i < m; i++) {
            int u = Find(e[i].u); int v = Find(e[i].v);
            if (u != v) {
                ans = e[i].w > ans ? e[i].w : ans;
                p[u] = v;
                cnt_edge++;
                if (cnt_edge == n - s) {
                    return ans;
                }
            }
        }
    }
}

int s, p;
Point<int> point[MAXV];
Graph<double> graph;

int main() {
    int tot_case;
    scanf("%d", &tot_case);
    while (tot_case--) {
        // Input.
        graph.Init();
        scanf("%d%d", &s, &p);
        for (int i = 0; i < p; i++) {
            scanf("%d%d", &point[i].x, &point[i].y);
        }
        // Compute each the length of edges.
        for (int i = 0; i < p; i++) {
            for (int j = 0; j < i; j++) {
                double w = Point<int>::GetDistance(point[i], point[j]);
                graph.AddEdge(i, j, w);
            }
        }
        // Output.
        printf("%.2lf\n", Kruskal::Go(graph.edge, p, graph.tot_edge, s));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值