HDU 2295 Radar (DLX可重复覆盖+二分)

该博客介绍了一种解决用最少数量的雷达覆盖所有城市的算法。通过二分枚举雷达的半径,构建覆盖表并判断是否能用少于K个雷达覆盖所有城市。当找到满足条件的最小半径时,二分搜索结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

给出N个城市的坐标,M个雷达的坐标,求用少于K个雷达站覆盖所有城市的最小雷达半径(所有启用的雷达站的半径都一样)

思路:
二分枚举半径,每次枚举得到一组覆盖表,对这张表进行覆盖,看能不能用少于K个雷达覆盖所有城市。

若能向左(半径减小)继续二分,若不能向右(半径增大)继续二分。直到精度误差满足要求二分停止。

代码:

#include <bits/stdc++.h>
#define mid (le+ri)/2;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=230;
const double eps=1e-7;
int L[maxn*maxn],R[maxn*maxn],U[maxn*maxn],D[maxn*maxn];//节点的上下左右四个方向的链表
int C[maxn*maxn],H[maxn],cnt[maxn],vis[maxn];//C列H行cnt列链表中元素个数
int n,m,id,fans,T,lim,cut;
double le,ri,ans;
typedef struct Node{
    int x;int y;
    Node(int xx=0,int yy=0){x=xx;y=yy;}
}Node;
Node city[60];
Node rad[60];
void init(){
    for(int i=0;i<=cut;i++){
        cnt[i]=0;U[i]=D[i]=i;
        L[i+1]=i;R[i]=i+1;
    }
    R[cut]=0;id=cut+1;
    memset(H,-1,sizeof(H));
}
void Link(int r,int c){
    cnt[c]++;C[id]=c;
    U[id]=U[c];D[U[c]]=id;
    D[id]=c;U[c]=id;
    if(H[r]==-1) H[r]=L[id]=R[id]=id;
    else{
        L[id]=L[H[r]];R[L[H[r]]]=id;
        R[id]=H[r];L[H[r]]=id;
    }
    id++;
}
void Remove(int Size){
    for(int j=D[Size];j!=Size;j=D[j])
        L[R[j]]=L[j],R[L[j]]=R[j];
}
void Resume(int Size){
    for(int j=D[Size];j!=Size;j=D[j])
        L[R[j]]=R[L[j]]=j;
}
int h()
{
    int sum=0;
    memset(vis,0,sizeof(vis));
    for(int i=R[0];i;i=R[i]){
        if(vis[i]) continue;
        sum++;
        for(int j=D[i];j!=i;j=D[j]){
            for(int k=R[j];k!=j;k=R[k])
                vis[C[k]]=1;
        }
    }
    return sum;
}
void Dance(int k){
    int mm=maxn,pos;
    if(k+h()>=fans) return;
    if(!R[0]){
        if(k<fans) fans=k;
        return;
    }
    for(int i=R[0];i;i=R[i])
        if(mm>cnt[i]) mm=cnt[i],pos=i;

    for(int i=D[pos];i!=pos;i=D[i])
    {
        Remove(i);
        for(int j=R[i];j!=i;j=R[j]) Remove(j);
        Dance(k+1);
        for(int j=R[i];j!=i;j=R[j]) Resume(j);
        Resume(i);
    }
}
int get(double dis){
    init();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
        if(sqrt((rad[i].x-city[j].x)*(rad[i].x-city[j].x)+(rad[i].y-city[j].y)*(rad[i].y-city[j].y))<=dis){
            Link(i+1,j+1);
        }
    }

    fans=lim+1;
    Dance(0);
    return fans;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&m,&n,&lim);
        cut=m;
        for(int i=0;i<m;i++)
            scanf("%d%d",&city[i].x,&city[i].y);
        for(int i=0;i<n;i++)
            scanf("%d%d",&rad[i].x,&rad[i].y);
        le=0;
        ri=sqrt(1001*1001);
        while((ri-le)>eps){
            ans=mid;
            if(get(ans)<=lim){
                ri=mid;
            }else{
                le=mid;
            }
        }
        printf("%.6lf\n",ans);
    }
}

N cities of the Java Kingdom need to be covered by radars for being in a state of war. Since the kingdom has M radar stations but only K operators, we can at most operate K radars. All radars have the same circular coverage with a radius of R. Our goal is to minimize R while covering the entire city with no more than K radars.
Input
The input consists of several test cases. The first line of the input consists of an integer T, indicating the number of test cases. The first line of each test case consists of 3 integers: N, M, K, representing the number of cities, the number of radar stations and the number of operators. Each of the following N lines consists of the coordinate of a city. 
Each of the last M lines consists of the coordinate of a radar station. 

All coordinates are separated by one space. 
Technical Specification 

1. 1 ≤ T ≤ 20 
2. 1 ≤ N, M ≤ 50 
3. 1 ≤ K ≤ M 
4. 0 ≤ X, Y ≤ 1000 
Output
For each test case, output the radius on a single line, rounded to six fractional digits.
Sample Input
1
3 3 2
3 4
3 1
5 4
1 1
2 2
3 3
Sample Output
2.236068


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值