zoj 1914 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.


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

题目大意:

    国防部想在北部的前哨之间建立一个无线网络连接这些前哨。在建立网络时使用了两种不同的通信技术:每个前哨有一个无线电收发器,有一些前哨还有一个卫星频道。
    任何两个拥有卫星频道的前哨之间可以直接通过卫星进行通信,而且卫星通信跟距离和位置无关。否则,两个前哨之间通过无线电收发器进行通信,并且这两个前哨之间的距离不能超过D,这个D值取决于无线电收发器的功率。功率越大,D值也就越大,但是价格也越高。出于对购买费用和维护费用的考虑,所有使用无线电接收器进行通信的前哨的收发器必须相同,即D值相同。
    你的任务是计算无线电收发器D值的最小值,且保证每两个前哨之间至少有一条通信线路(直接或间接地连接这两个前哨)。

     

本题就是,求出最小生成树的时候,顺便记录生成树的权值,然后从小到大排序,选择p-s条边。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int Father[110];
struct node
{
    int a;
    int b;
    double w;
};
int cmp(node a,node b)
{
    return a.w<b.w;
}
int Find(int x)
{
    while (x!=Father[x])
    {
        x=Father[x];
    }
    return x;
}
void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if(x!=y)
    {
        Father[x]=y;
    }
}
int main()
{
    int n,s,p;;
    cin>>n;
    int x[505];
    int y[505];
    node num[500*500+10];
    while (n--)
    {
        cin>>s>>p;
        for (int i=0; i<=p; i++)
        {
           Father[i]=i;
        }
        for (int i=0; i<p; i++)
        {
            cin>>x[i]>>y[i];
        }
        int ans=0;
        for (int i=0; i<p; i++)
        {
            double a;
            for (int j=i+1; j<p; j++)
            {
                
                a=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);  //两个点距离的平方
                num[ans].a=i;
                num[ans].b=j;
                num[ans].w=a;
                ans++;
            }
        }
        double data[505];
        int k=0;
        sort(num, num+ans, cmp);
        for (int i=0; i<ans; i++)
        {
            int a=Find(num[i].a);
            int b=Find(num[i].b);
            if(a!=b)
            {
                Union(a, b);
                data[k++]=num[i].w;
                if(p-s==k)    //选择p-s条边
                {
                    break;
                }
            }
        }
        printf("%.2f\n",sqrt(data[k-1]));
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值