UVa:10369 Arctic Network

50 篇文章 0 订阅

事实上就是求最小生成树中第m-(s-1)条最长的边。

在有前哨已经拥有卫星频道的情况下,如果某一个前哨使用卫星频道那么意味着删掉与该点关联的最长的一条边。用s个频道可以删掉(s-1)条最长边,所以答案就是第m-(s-1)条最长的边。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MAXN 505
using namespace std;
struct Edge
{
    int a,b;
    double weight;
};
struct Point
{
    int x,y;
};
int father[MAXN];
int find(int p)
{
    return p==father[p]?p:(father[p]=find(father[p]));
}
double Dist(Point a,Point b)
{
    double ll=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    return sqrt(ll);
}
bool cmp(Edge a,Edge b)
{
    return a.weight<b.weight;
}
Edge e[MAXN*MAXN];
Point po[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int s,p;
        scanf("%d%d",&s,&p);
        for(int i=0; i<p; ++i)
            scanf("%d%d",&po[i].x,&po[i].y);
        int N=0,cou=0;
        for(int i=0; i<p; ++i)
            for(int j=i+1; j<p; ++j)
            {
                e[N].a=i;
                e[N].b=j;
                e[N++].weight=Dist(po[i],po[j]);
            }

        sort(e,e+N,cmp);
        for(int i=0; i<=p; ++i)
            father[i]=i;
        int _id[MAXN]= {0};
        for(int i=0; i<N; ++i)
        {
            int ta=find(e[i].a),tb=find(e[i].b);
            if(ta!=tb)
            {
                father[ta]=tb;
                _id[cou++]=i;
            }
        }
        printf("%.2lf\n",e[_id[cou-s]].weight);
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值