POJ 2349 Arctic Network+Kruskal最小生成树

本文介绍了一个基于无线网络连接的问题——如何利用卫星通道和无线电技术在多个北部哨所间建立通信网络,并提供了一种通过最小生成树算法求解该问题的方法。

Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18714 Accepted: 5921

Description

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

Source


题意:

首先是样例数T

输入卫星个数S,输入村庄个数P,然后输入P个村庄的坐标。

卫星的作用就是让两个村庄可以无视距离的通讯,*(x个村庄通信需要x个卫星)

让你求为了让所有村庄通信,除了卫星的最小的两个村庄距离。

解题思路:

直接500个村庄,完全图的边数在合理范围内,所以求出每两个村庄的距离

跑一遍最小生成树,注意这个最小生成数加边加到n-s就够了,输出那个边就可以了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1000005 ;
int s,p,n,m;
int u[maxn],v[maxn];
double w[maxn] ;
int xx[maxn],yy[maxn];
int pp[maxn];
int r[maxn];
int fid(int x){return pp[x]==x?x:pp[x]=fid(pp[x]);}
int cmp(const int i,const int j){return w[i]<w[j] ;}
double kruskal(){
    double ret =0;
    for(int i=0;i<n;i++)pp[i]=i;
    for(int i=0;i<m;i++)r[i]=i;
    sort(r,r+m,cmp) ;
    int cnt=0;
    for(int i=0;i<m;i++){
        if(cnt==n-s){
            break ;
        }
        int e = r[i] ;
        int x = fid(u[e]) ;
        int y = fid(v[e]) ;
        if(x!=y){
            pp[x]=y ;
            cnt++ ;
            ret = w[e] ;
        }
    }
    return ret ;

}
int main(){
    int t ;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&s,&p);
        n=p;
        for(int i=0;i<p;i++){
            scanf("%d%d",&xx[i],&yy[i]);
        }
        int c=0;
        for(int i=0;i<p;i++){
            for(int j=i+1;j<p;j++){
                u[c]=i;
                v[c]=j;
                w[c++]=sqrt(pow((double)(xx[i]-xx[j]),2)+pow((double)(yy[i]-yy[j]),2));
            }
        }
        m=c;
        double ans = kruskal() ;
        printf("%.2f\n",ans);
    }
    return 0;
}





已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 常见问题解答 网页打开速度慢或者打不开网页? 受到多种因素的影响,对于非会员用户我们无法提供最优质的服务。 如果您希望得到最棒的体验,请至大会员页面("右上角菜单 → 大会员")根据说明操作。 请注意:受制于国际网络的诸多不确定性,我们无法对任何服务的可靠性做出任何保证。 如果出现了网络连接相关的问题,我们建议您先等待一段时间,之后再重试。 如果您在重试后发现问题仍然存在,请联系我们,并说明网络问题持续的时间。 图片下载后无法找到? 打开"右上角菜单 → 更多 → 修改下载路径",在弹出的对话框中可以看到当前图片的保存路径。 此外,由于网络因素,在保存图片之后,等待屏幕下方出现"已保存到..."后,才能在本地找到图片。 如何更改图片保存的目录? 请参见"右上角菜单 → 更多 → 修改下载路径"。 翻页不方便? 在点进某个图片后,通过在图片上向左或向右滑动,即可翻页查看下一个作品。 如何保存原图/导出动图? 长按图片/动图,在弹出的菜单中选择保存/导出即可。 输入账号密码后出现"进行人机身份验证"? 此为pixiv登陆时的验证码,请按照要求点击方框或图片。 在pxvr中注册pixiv账号后,收到验证邮件,无法访问邮件中的验证链接? 请复制邮件中的链接,打开pxvr中的"右上角菜单 → 输入地址"进行访问。 能否自动将页面内容翻译为汉语? 很抱歉,pxvr暂不提供语言翻译服务。 图片下载类型是否可以选择? 能否批量下载/批量管理下载? 已支持批量下载多图作品中的所有原图:找到一个多图作品,进入详情页面后,点击图片进入多图浏览模式,长按任意一张图片即可看到批量下载选项。 关于上述其他功能,我们...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值