poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network

题目传送

Sol:

方法一:

贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案。

code:

#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define IL inline
#define RG register
#define DB double
#define LL long long
using namespace std;

IL int gi() {
    RG int x=0,p=1; RG char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') p=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
    return x*p;
}

const int N=507;

DB d[N];
int n,m,tot,fa[N];

struct DOT{int x,y;}a[N];
struct EDGE{int x,y;DB d;}e[N*N<<1];

IL bool operator <(EDGE A,EDGE B) {return A.d<B.d;}

IL DB dis(int s,int b) {
    return sqrt(1.0*(a[s].x-a[b].x)*(a[s].x-a[b].x)+(a[s].y-a[b].y)*(a[s].y-a[b].y));
}

int getfa(int x) {return fa[x]==x?x:fa[x]=getfa(fa[x]);}

IL void Kruskal() {
    RG int i,fx,fy,cnt=0;
    for(i=1;i<=n;++i) fa[i]=i;
    for(i=1;i<=tot;++i) {
        fx=getfa(e[i].x),fy=getfa(e[i].y);
        if(fx==fy) continue;
        fa[fx]=fy,d[++cnt]=e[i].d;
        if(cnt==n-1) break;
    }
    printf("%.2lf\n",d[n-m]);//poj 上如用G++交代码,则需把%lf改为%f
}

int main()
{
    RG int i,j,T=gi();
    while(T--) {
        m=gi(),n=gi(),tot=0;
        for(i=1;i<=n;++i) a[i].x=gi(),a[i].y=gi();
        for(i=1;i<=n;++i)
            for(j=i+1;j<=n;++j)
                e[++tot]=(EDGE){i,j,dis(i,j)};
        sort(e+1,e+tot+1);
        Kruskal();
    }
    return 0;
}
//poj 145ms

方法二:

考虑二分答案,满足条件的点连好边,然后dfs一下有几个连通块,有多少连通块就需要多少卫星,和m比较即可。

相比方法一,code又慢又长。。。

code:

#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#define IL inline
#define RG register
#define DB double
#define LL long long
using namespace std;

IL int gi() {
    RG int x=0,p=1; RG char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') p=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
    return x*p;
}

const int N=505;
const DB eps=1e-4;

DB l,r,mid;
int n,m,cnt,tot,vis[N],head[N];

struct DOT{int x,y;}a[N];
struct EDGE{int next,to;}e[N*N<<1];

IL void New_case() {
    tot=cnt=0;
    memset(&e,0,sizeof(e));
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
}

IL void make(int x,int y) {
    e[++tot]=(EDGE){head[x],y},head[x]=tot;
    e[++tot]=(EDGE){head[y],x},head[y]=tot;
}

IL DB dis(int s,int b) {
    return 1.0*sqrt((a[s].x-a[b].x)*(a[s].x-a[b].x)+(a[s].y-a[b].y)*(a[s].y-a[b].y));
}

IL void dfs(int x) {
    RG int i,y;
    vis[x]=1;
    for(i=head[x];i;i=e[i].next)
        if(!vis[y=e[i].to]) dfs(y);
}

IL int check(DB val) {
    RG int i,j;
    New_case();
    for(i=1;i<=n;++i)
        for(j=i+1;j<=n;++j)
            if(dis(i,j)<=val) make(i,j);
    for(i=1;i<=n;++i)
        if(!vis[i]) dfs(i),++cnt;
    return cnt<=m;
}

int main()
{
    RG int i,T=gi();
    while(T--) {
        m=gi(),n=gi();
        for(i=1;i<=n;++i) a[i].x=gi(),a[i].y=gi();
        l=0,r=1e9;
        while(r-l>=eps) {
            mid=(l+r)/2;
            if(check(mid)) r=mid;
            else l=mid;
        }
        printf("%.2lf\n",r);
    }
    return 0;
}
// poj 1400多ms

原文地址:https://www.cnblogs.com/Bhllx/p/11247350.html

时间: 2024-08-01 15:29:19

poj 2349 Arctic Network MST/二分答案的相关文章

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

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

题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部队. 2,用信号收发器可以链接周围d米以内的部队. 现在有s个卫星信道,问d最小是多少时能连接全部的警戒部队? 解题思路: 我是用求最小生成树,记录路径长度,对路径长度排序后,第k长的边就是答案, 但是队友是用最小k度限制生成树,因为我的方法它证明不了,也推翻不了~~~~, 最后我下去仔细想了想反证

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

题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两个前哨之间的距离不超过D,才能通过无线电进行通信.求出能使所有前哨都能直接或间接通信的最小的D. 解题思路:题目要求使所有前哨都能直接或间接通信,那么相当于使n个点相连,至少需要n-1条边.可以将n个点分为s个团,每个团内部时无限通信,团与团之间通过卫星通信.那么就相当于用s个卫星装置建立s-1条边

poj 2349 Arctic Network(prime)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25165   Accepted: 7751 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are t

POJ 2349 Arctic Network (最小生成树)

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 a

POJ2349:Arctic Network(二分+最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题目链接:http://poj.org/problem?id=2349 Description: The Department of National Defence (DND) wishes to connect several northern outposts by a wireless net