Arctic

POJ-2349

  • 这题是最小生成树的变形题目。题目的意思是已经有s个卫星频道,这几个卫星频道可以构成一部分的网络,而且不用费用,剩下的需要靠d的卫星接收器。题目要求的就是最小生成树中,最大的边的长度。
  • 题目中的传入kruskal函数里面的sn表示还需要连接的顶点个数,因为剩下的可以使用卫星频道来连接。
  • 剩下的就是kruskal的问题了,这里通过当前最大边来返回答案。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=505;
const int maxm=300000;
const double INF=0X3F3F3F3F;
int s,n;
int e;
struct node{
int from;
int to;
double cost;
bool operator<(const node& t)const{
return cost<t.cost;
}
};
node edge[maxm];
int x[maxn];
int y[maxn];
int set[maxn];
int find(int x){
return set[x]==x?set[x]:set[x]=find(set[x]);
}
double kruskal(int sn){
sort(edge,edge+e);
for(int i=1;i<=n;i++){
set[i]=i;
}
double maxs=-INF;
int ans=0;
for(int i=0;i<e;i++){
int from=edge[i].from;
int to=edge[i].to;
double cost=edge[i].cost;
int x=find(from);
int y=find(to);
if(x!=y){
set[x]=y;
maxs=max(cost,maxs);
ans++;
}
if(ans==sn-1){
break;
}
}
if(ans<sn-1){
return -1;
}else{
return maxs;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
e=0;
scanf("%d%d",&s,&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
double cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
edge[e++]=node{i,j,cost};
}
}
printf("%.2f\n",kruskal(n-s+1));
}
return 0;
}

POJ-2349(kruskal算法+最小生成树中最大边的长度)的更多相关文章

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

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

  2. POJ 1797 kruskal 算法

    题目链接:http://poj.org/problem?id=1797 开始题意理解错.不说题意了. 并不想做这个题,主要是想测试kruskal 模板和花式并查集的正确性. 已AC: /* 最小生成树 ...

  3. POJ 2349 Arctic Network(最小生成树+求第k大边)

    题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...

  4. Kruskal算法-最小生成树

    2017-07-26  10:32:07 writer:pprp Kruskal算法是根据边的加权值以递增的方式,一次找出加权值最低的边来建最小生成树:并且每次添加的边不能造成生成树有回路,直到找到N ...

  5. poj 2485 (kruskal算法)

    /*kruskal算法*/ #include <iostream> //#include <fstream> #include <algorithm> using ...

  6. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  7. poj 2349 Arctic Network(最小生成树的第k大边证明)

    题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部 ...

  8. poj 2349 Arctic Network 最小生成树,求第k大条边

    题目抽象出来就是有一些告诉坐标的通信站,还有一些卫星,这些站点需要互相通信,其中拥有卫星的任意两个站可以不用发射器沟通,而所有站点的发射器要都相同,但发射距离越大成本越高. 输入的数据意思: 实例个数 ...

  9. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

随机推荐

  1. Codeforces Round #651 (Div. 2) B. GCD Compression (构造)

    题意:有一个长度为\(2n\)的数组,删去两个元素,用剩下的元素每两两相加构造一个新数组,使得新数组所有元素的\(gcd\ne 1\).输出相加时两个数在原数组的位置. 题解:我们按照新数组所有元素均 ...

  2. GYM101810 ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018) M. Greedy Pirate (LCA)

    题意:有\(n\)个点,\(n-1\)条边,每条边正向和反向有两个权值,且每条边最多只能走两次,有\(m\)次询问,问你从\(u\)走到\(v\)的最大权值是多少. 题解:可以先在纸上画一画,不难发现 ...

  3. Kubernets二进制安装(18)之安装部署Heapster

    1.下载heapster镜像 在运维主机(mfyxw50.mfyxw.com)执行 [root@mfyxw50 ~]# docker pull quay.io/bitnami/heapster:1.5 ...

  4. 【转】K8S中部署Helm

    K8S中的包管理工具 1. 客户端Helm(即Helm)  通过脚本安装:curl https://raw.githubusercontent.com/helm/helm/master/scripts ...

  5. vue-router离开当前页面提示未保存,解决在使用beforeRouteLeave弹窗多次闪现问题

    在使用beforeRouteLeave时要注意两点: 1. next(false)阻止路由继续进行,若不先阻止,会多次执行守卫中的代码 2. 利用setTimeout延时触发弹窗,避免出现闪现情况

  6. 你不知道的 JS (系列丛书) - 第二版

    你不知道的 JS (系列丛书) - 第二版 You Don't Know JS (book series) - 2nd Edition https://github.com/learning-js-b ...

  7. React Query & SWR

    React Query & SWR HTTP request all in one solution React Query Hooks for fetching, caching and u ...

  8. website URL & UTM

    website URL & UTM UTM user track message utm_source https://zhuanlan.zhihu.com/p/143473571?utm_s ...

  9. React Native 三端同构

    React Native 三端同构 https://www.ibm.com/developerworks/cn/web/wa-universal-react-native/index.html rea ...

  10. Taro Advanced

    Taro Advanced aro 代码与小程序代码混写 https://nervjs.github.io/taro/docs/hybrid.html https://github.com/NervJ ...