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

题意:有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。

分析:这是一个最小生成树问题。P个哨所最多用P-1条边即可连起来,而S颗卫星可以代替S-1条边,基于贪心思想,代替的边越长,求得的D就越小。所以可以用一个数组保存加入最小生成树的边的长度,共有P-1条边,把前S-1条较长的边代替掉,剩下的边中最长的即为所求,所以Kru加边至第(P-1)-(S-1)=P-S条边即为所求。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; int f[],x[],y[]; struct Node{
int u,v;
double w;
}edge[]; bool cmp(Node a,Node b)
{
return a.w<b.w;
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} double kru(int s,int n,int m)
{
int i;
for(i=;i<=n;i++){
f[i]=i;
}
sort(edge+,edge+m+,cmp);
int cnt=;
double ans=;
for(i=;i<=m;i++){
int u=edge[i].u;
int v=edge[i].v;
double w=edge[i].w;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans=w;
f[fv]=fu;
cnt++;
}
if(cnt==n-s) break;
}
if(cnt<n-s) return -;
else return ans;
} int main()
{
int t,s,n,m,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&s,&n);
for(i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
memset(edge,,sizeof(edge));
int m=;
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
edge[++m].w=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
edge[m].u=i;
edge[m].v=j;
}
}
printf("%.2f\n",kru(s,n,m));
}
return ;
}

POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru的更多相关文章

  1. ZOJ 1914 Arctic Network (POJ 2349) MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 http://poj.org/problem?id=2349 题目大 ...

  2. ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST

    ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...

  3. POJ 2349 Arctic Network (最小生成树)

    Arctic Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  4. poj 2349 Arctic Network

    http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  5. Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9557   Accepted: 3187 De ...

  6. POJ 2349 Arctic Network (最小生成树)

    Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...

  7. [poj2349]Arctic Network(最小生成树+贪心)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17758   Accepted: 5646 D ...

  8. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  9. Poj(2349),最小生成树的变形

    题目链接:http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 中文WebFont探索

    本文主要讲中文webFont的相关知识,包含了业界现状.WebFont优势.实现方案等. 一 业界使用WebFont现状 1.1 英文WebFont使用现状 英文版已使用非常广泛.比较有名的字体库:G ...

  2. 解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题

    最新解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题,由系统下载吧率先分享: 有些用户在使用Windows7系统过程中,碰到到win7打印机共享出现“无法保存打印机设置 ...

  3. pyinstaller使用

    python pyinstaller.py [-Fw] ???.py -F 将相关配件(dll.oxc)合成到单个exe文件 -w exe启动时不打开console窗口

  4. 常用js方法函数

    常用方法函数 1.深复制 // 1.深复制 function deepCopy(source) { var result = {}; for (var key in source) { result[ ...

  5. AiCloud 2.0 AT开发文档【转】

    本文转载自:http://wiki.ai-thinker.com/aicloud/docs/atdevelop AT指令一览表 AiCloud AT指令   指令 描述 AT+CLDSTART 启动云 ...

  6. android自定义控件(三) 增加内容 自定义属性 format详解

    转自 http://www.gisall.com/html/35/160435-5369.html 1. reference:参考某一资源ID. (1)属性定义: <declare-stylea ...

  7. CentOS 性能监控之nmon

    工具集: Nmon  性能数据收集分析工具Nmon analyser 性能数据分析工具,excel文件nmon_x86_sles10  Nmon在x86_sles10下二进制执行文件 nmon概述 n ...

  8. forEach、for in 和for of的区别

    forEach  不能使用break return 结束并退出循环 for in 和 for of 可以使用break return: for in遍历的是数组的索引(即键名),而for of遍历的是 ...

  9. 自己实现的vector

    #include <iostream> #include <memory> using std::cout; using std::endl; using std::alloc ...

  10. bzoj 4278 Tasowanie —— 后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4278 每次取两个后缀中字典序较小的那个的首字符: 注意超出去的部分是 inf 而不是 0,因 ...