POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru
Arctic Network
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
Output
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的更多相关文章
- ZOJ 1914 Arctic Network (POJ 2349) MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 http://poj.org/problem?id=2349 题目大 ...
- 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 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- poj 2349 Arctic Network
http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- 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 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...
- [poj2349]Arctic Network(最小生成树+贪心)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17758 Accepted: 5646 D ...
- 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 ...
- Poj(2349),最小生成树的变形
题目链接:http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total S ...
随机推荐
- Dubbo服务集群,常见容错机制:failover ,failsafe,failfase ,failback,forking
http://blog.csdn.net/hongweigg/article/details/52925920 http://m.blog.csdn.net/article/details?id=51 ...
- 在java项目中使用protobuf
1 通用方式 第一步,定义数据结构 第二步,使用protoc.exe生成java代码 第三步,序列化 第四步,反序列化 2 grpc方式 grpc官方推荐的方式,通过maven插件来生成java代码. ...
- OCR光学字符识别--STN-OCR 测试
1.同文章中建议的使用ubuntu-python隔离环境,真的很好用 参照:http://blog.topspeedsnail.com/archives/5618启动虚拟环境:source env/b ...
- 【题解】[P3557 POI2013]GRA-Tower Defense Game
[题解][P3557 POI2013]GRA-Tower Defense Game 这道题是真的** 根据题目给的\(k\),可以知道,我们随便放塔,只要不全放一起,一定是一种合法的方案. 直接枚举就 ...
- 您使用的是不受支持的命令行标记 chrome
检查 chrome://flags/#extensions-on-chrome-urls 是否开启 开启了的话就关掉检查 启动chrome的快捷方式是否在目标后有额外的参数 有就删了 在浏览器中输入c ...
- imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。
php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...
- 基于springboot的RestTemplate、okhttp和HttpClient对比
1.HttpClient:代码复杂,还得操心资源回收等.代码很复杂,冗余代码多,不建议直接使用. 2.RestTemplate: 是 Spring 提供的用于访问Rest服务的客户端, RestTem ...
- P3746 [六省联考2017]组合数问题
P3746 [六省联考2017]组合数问题 \(dp_{i,j}\)表示前\(i\)个物品,取的物品模\(k\)等于\(r\),则\(dp_{i,j}=dp_{i-1,(j-1+k)\%k}+dp_{ ...
- centos7在VMware下配置网络连接
安装成功以后,首先更改vmwar的虚拟网络设置 1.参考连接:http://www.cnblogs.com/liongis/p/3265458.html 2.然后将虚拟机的设置里面将网络配置的连接方式 ...
- web前端js过滤敏感词
web前端js过滤敏感词 这里是用文本输入框还有文本域绑定了失去焦点事件,然后再遍历敏感词数组进行匹配和替换. var keywords=["阿扁","呵呵", ...