博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2349——Arctic Network(最小生成树+prim)
阅读量:2343 次
发布时间:2019-05-10

本文共 2542 字,大约阅读时间需要 8 分钟。

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

给出p个哨所的坐标,哨所之间的通讯距离不能小于d,或者可以直接用卫星通讯,这样就不需要考虑距离,求最小的d。

如果没有卫星通讯,那就是个很裸的最小生成树,加了卫星后,意味着最小生成树中最长的s-1条边可以去掉,然后剩下的最长的边就是d。
思路就是记录求最小生成树过程中产生的边,排序然后输出第s大的边。
一开始数组开了200居然超时,好气啊

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;struct Node{ double x,y;};Node p[MAXN];int n,vis[MAXN],ans;double map[MAXN][MAXN],dis[MAXN];double getdis(int a,int b){ double x=p[a].x-p[b].x,y=p[a].y-p[b].y; return sqrt(x*x+y*y);}double m[MAXN];void prim(){ int i,j,pos; double min; ans=0; memset(vis,0,sizeof(vis)); vis[1]=1,pos=1; for(i=1; i<=n; ++i) if(i!=pos) dis[i]=map[pos][i]; for(i=1; i
map[pos][j]) dis[j]=map[pos][j]; } //return ans;}int main(){ int s,t; scanf("%d",&t); while(t--) { scanf("%d%d",&s,&n); for(int i=1; i<=n; ++i) scanf("%lf%lf",&p[i].x,&p[i].y); for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) map[i][j]=getdis(i,j); prim(); sort(m,m+ans); printf("%.2lf\n",m[n-s-1]); } return 0;}

转载地址:http://kpcvb.baihongyu.com/

你可能感兴趣的文章
冯诺依曼工作方式的基本特点是____
查看>>
下列关于文件索引结构的叙述中,哪些是正确的?
查看>>
虚拟存储的容量受到下列哪一个因素的限制影响最大?
查看>>
关于域名和IP描述正确的是?
查看>>
哪些字段适合建立索引?
查看>>
关于group by子句的作用描述正确的是?
查看>>
执行"int x=1;int y=~x;"语句后,y的值为?-----取反运算,补码
查看>>
内存按字节编址,地址区间为[90000H,CFFFFH],若用32K*8bit的存储器芯片构成该内存,需要__块
查看>>
直接,间接,立即三种寻址方式指令的执行速度,由快至慢的排序是____
查看>>
设计文件系统时应尽量减少访问磁盘的次数,以提高文件系统的性能.下列各种措施中,哪些可以减少磁盘服务时间?
查看>>
memove 的实现
查看>>
下面有关C++中为什么用模板类的原因,描述错误的是?
查看>>
计算类的大小--字节对齐
查看>>
重载、覆盖、隐藏
查看>>
下面有关c++线程安全,说法错误的是?
查看>>
下面对 C++ 静态数据成员的描述中,正确的是?
查看>>
C++类体系中,不能被派生类继承的有?
查看>>
下面有关malloc和new,说法错误的是?
查看>>
在32位小端的机器上,如下代码输出是什么:
查看>>
switch(c)语句中,c不可以是什么类型()
查看>>