Uva - 10369 - Arctic Network

题意:S个点,P个卫星,每个点要么放卫星,要么放电台,使所有点连通,两个卫星之间可以连无限远,但两个电台之间只能在D距离内有效,求最小的D(1 <= S <= 100,S < P <= 500)。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22166

——>>生成最小生成树后,一个问题,理解题意:

卫星不放在两点之间,而是放在点上,所以S个卫星能连通S个点,卫星之间有S(S-1)/2条信道,但对点来说只是连通了S-1条边,而不是S条边。

#include <cstdio>
#include <cmath>
#include <queue>

using namespace std;

const int maxn = 500 + 10;
int N, S, P, fa[maxn];
double ret[maxn];

struct Point{
    double x;
    double y;
}p[maxn];

double Dis(Point A, Point B){
    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}

struct node{
    int u;
    int v;
    double dis;
    bool operator < (const node& e) const{
        return dis > e.dis;
    }
};

void init(){
    for(int i = 1; i <= P; i++) fa[i] = i;
}

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

bool Union(int x, int y){
    int newx = Find(x);
    int newy = Find(y);
    if(newx == newy) return 0;
    fa[newy] = newx;
    return 1;
}

void read(){
    scanf("%d%d", &S, &P);
    init();
    for(int i = 1; i <= P; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
}

void Kruscal(){
    priority_queue<node> pq;
    for(int i = 1; i <= P; i++)
        for(int j = i+1; j <= P; j++)
            pq.push((node){i, j, Dis(p[i], p[j])});
    int m = 0;
    while(!pq.empty()){
        node no = pq.top(); pq.pop();
        if(Union(no.u, no.v)) ret[m++] = no.dis;
    }
    printf("%.2f\n", ret[m-S]);
}

int main()
{
    scanf("%d", &N);
    while(N--){
        read();
        Kruscal();
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值