uva 10369 - Arctic Network(最小生成树)

Problem C: Arctic Network

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.

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). 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

这道题最初的想法是二分d,判断在d的条件下,图中的联通分量个数是否小于等于s,这样的复杂度就是n2logd,但这和生成树就没什么关系了。

后来偶然看到篇论文有这道题目,他给出了定理:如果去掉所有权值大于d的边后,最小生成树被分割成为k个连通支,图也被分割成为k个连通支。

这样就可以推得::最小生成树的第k长边就是问题的解。

这就把二分试探找d变成了直接通过定理得到,只需一次最小生成树即可。

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

const int maxn = 500 + 10;
const int INF = 1000000000;

struct Edge {
  int x, y;
  double d;
  bool operator < (const Edge& rhs) const {
    return d < rhs.d;
  }
};

int n,m,s;
int fa[maxn];
int x[maxn],y[maxn];
Edge e[maxn*maxn];

int find(int x){return x == fa[x]?x:fa[x] = find(fa[x]);}

double Dist(int i,int j){return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}

double MST(){
    int cnt = 0;
    double ret;
    for(int i = 0;i < n;i++) fa[i] = i;
    sort(e,e+m);
    for(int i = 0;i < m;i++){
        int a = e[i].x;
        int b = e[i].y;
        double d = e[i].d;
        int Y = find(b);
        int X = find(a);
        if(X != Y){
            fa[X] = Y;
            if(++cnt == n-s) ret = d;
            if(cnt == n-1) break;
        }
    }
    return ret;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        m = 0;
        scanf("%d%d",&s,&n);
        for(int i = 0;i < n;i++)
            scanf("%d%d",&x[i],&y[i]);
        for(int i = 0;i < n;i++)
            for(int j = i+1;j < n;j++)
              e[m++] = (Edge){i,j,Dist(i,j)};
        double ans = MST();
        printf("%.2lf\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值