Arctic Network POJ 2349 (最小生成树思想)
Description
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
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
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个哨所和S个卫星,要建立所有哨所之间的联系。有卫星的两个哨所之间可以任意通信;否则一个哨所只能和距离它小于等于D的哨所通信。给出P个哨所的坐标,求D的最小值。
解题思路:为了使这P个哨所建立起联系,我们需要树状图,所以需要使用最小生成树的算法,将所有点建立起联系。但是为了所得到的D最小,对于最小生成树种那些边长较长的边,我们可以使用卫星进行联系,有S个卫星,这样就将最小生成树划分为了S个割集,割集之间使用卫星联系,割集之内使用无线联系,求割集内的点之间的最大值即为最小的D。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxs 550
using namespace std;
int pre[maxs];
int m,k;
int s,p;
double dis[maxs*maxs];
struct node///各个哨所的坐标
{
int x;
int y;
} a[maxs];
struct edge///哨所之间联系构成边
{
int u;
int v;
double w;
} e[maxs*maxs];
bool my_cmp(edge a,edge b)
{
return a.w<b.w;
}
bool my_cmp2(double a,double b)
{
return a>b;
}
double Getdis(node a,node b)///获得各个边的距离
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
int Find(int x)
{
if(x!=pre[x])
{
pre[x]=Find(pre[x]);
}
return pre[x];
}
void Union(int a,int b)
{
int x=Find(a);
int y=Find(b);
if(x>y)
{
pre[x]=y;
}
else
{
pre[y]=x;
}
}
void Kruskal()
{
memset(dis,0.0,sizeof(dis));
int i,counts;
counts=;
for(i=; i<=m; i++)
{
int fx=Find(e[i].u);
int fy=Find(e[i].v);
if(fx!=fy)
{
pre[fx]=fy;
counts++;
dis[counts]=e[i].w;
if(counts==p-)///p个点,p-1条边
{
break;
}
}
}
sort(dis+,dis+counts+,my_cmp2);
printf("%.2f\n",dis[s]);///最小的D为所有边中排除前S大后最大的那一个
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&p);
for(i=; i<=p; i++)
{
pre[i]=i;
}
m=;
for(i=; i<=p; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
for(i=; i<=p; i++)
{
for(j=i+; j<=p; j++)
{
m++;
e[m].u=i;
e[m].v=j;
e[m].w=Getdis(a[i],a[j]);
}
}
sort(e+,e+m+,my_cmp);
Kruskal();
}
return ;
}
Arctic Network POJ 2349 (最小生成树思想)的更多相关文章
- (最小生成树) Arctic Network -- POJ --2349
链接: http://poj.org/problem?id=2349 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1371 ...
- Arctic Network POJ - 2349
The Department of National Defence (DND) wishes to connect several northern outposts by a wireless n ...
- POJ2349:Arctic Network(二分+最小生成树)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28311 Accepted: 8570 题 ...
- POJ 2349 Arctic Network(贪心 最小生成树)
题意: 给定n个点, 要求修p-1条路使其连通, 但是现在有s个卫星, 每两个卫星可以免费构成连通(意思是不需要修路了), 问修的路最长距离是多少. 分析: s个卫星可以代替s-1条路, 所以只要求最 ...
- poj 2349(最小生成树应用)
题目链接:http://poj.org/problem?id=2349 思路:由于有S个专门的通道,我们可以先求一次最小生成树,然后对于最小生成树上的边从大到小排序,前S-1条边用S-1个卫星通道连接 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...
- poj 2349 Arctic Network
http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru
Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...
随机推荐
- package-lock.json 作用
package.json里面定义的是版本范围(比如^1.0.0),具体跑npm install的时候安的什么版本,要解析后才能决定,这里面定义的依赖关系树,可以称之为逻辑树(logical tree) ...
- Java并发编程(三)什么是线程池
什么是线程池 学习编程的小伙伴们会经常听到“线程池”.“连接池”这类的词语,可是到底“池”是什么意思呢?我讲个故事大家就理解了:在很久很久以前有一家银行,一年之中只有一个客户来办理业务,随着时间的推移 ...
- vue 复习(2)v-bind的应用 v-bind:classv-binf:style
dasdclass与style绑定v-bind 1. 绑定HTML Class 对象语法 有些时候我们想动态的切换class的类名.在原生的js或jq中我们就要通过事件来动态的改变class类名,但在 ...
- 构建WebGL目标时的内存考量
Memory Considerations when targeting WebGL 构建WebGL目标时的内存考量 Memory in Unity WebGL can be a constraini ...
- Delphi判断某个类是否实现了某个接口
通过TObject.GetInterface可以获得对象的实例实现某个接口,前提条件是必须实例化对象后才能运行GetInterface 下面的方法可获取类是否实现了某个接口,并返回接口的偏移: fun ...
- 电竞大数据平台 FunData 的系统架构演进
电竞大数据时代,数据对比赛的观赏性和专业性都起到了至关重要的作用.同样的,这也对电竞数据的丰富性与实时性提出了越来越高的要求. 电竞数据的丰富性从受众角度来看,可分为赛事.战队和玩家数据:从游戏角 ...
- Navicat 报错1251连接不成功Mysql
使用Navicat 连接数据库时候出现1251错误,解决方法. 1.首先打开mysql.exe,然后输入密码(mysql.exe可以在安装的位置搜索一下) 2.输入ALTER USER 'root'@ ...
- Xilinx与modelsim的仿真联调
关于Xilinx与modelsim的仿真联调,尤其是仿真环境的搭建,网上的信息都比较零散,我当初在联调的时候遇到比较多的问题,也是折腾了两天才弄好,下面的步骤我总结得非常详细,可以帮助大家少走弯路. ...
- flex tooltip css
<?xml version="1.0"?> <!-- tooltips/ToolTipStyleManager.mxml --> <mx:Applic ...
- 20155232 2016-2017-3 《Java程序设计》第3周学习总结
20155232 2016-2017-3 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 1.对象(Object):存在的具体实体,具有明确的状态和行为. 2.类( ...