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

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

题意:有 n 个点,m个圆,选择不超过 k 个圆覆盖所有的点,问最小圆的半径。

思路:二分答案,check用DLX判断需要选择的最少的圆的个数,与 k 比较。(注意模板中的行列都是从1开始编号的)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 55;
const int MAXN = N * N + 10;
const int MAXM = N * N + 10;
const int maxnode = MAXN * MAXM;

struct DLX {
    int n, m, siz;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN], S[MAXM];
    int ansd;
    void init(int _n, int _m) {
        n = _n;
        m = _m;
        for(int i = 0; i <= m; ++i) {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0, L[0] = m;
        siz = m;
        for(int i = 1; i <= n; ++i) H[i] = -1;
    }
    ///行列编号从1开始
    void Link(int r, int c) {
        ++S[Col[++siz] = c];
        Row[siz] = r;
        D[siz] = D[c];
        U[D[c]] = siz;
        U[siz] = c;
        D[c] = siz;
        if(H[r] < 0) H[r] = L[siz] = R[siz] = siz;
        else {
            R[siz] = R[H[r]];
            L[R[H[r]]] = siz;
            L[siz] = H[r];
            R[H[r]] = siz;
        }
    }
    void remove(int c) {
        for(int i = D[c]; i != c; i = D[i])
            L[R[i]] = L[i], R[L[i]] = R[i];
    }
    void resume(int c) {
        for(int i = U[c]; i != c; i = U[i])
            L[R[i]] = R[L[i]] = i;
    }
    bool vis[MAXM];
    ///剪枝优化
    int f() {
        int ret = 0;
        for(int c = R[0]; c != 0; c = R[c]) vis[c] = 1;
        for(int c = R[0]; c != 0; c = R[c]) {
            if(vis[c]) {
                ret++;
                vis[c] = 0;
                for(int i = D[c]; i != c; i = D[i])
                    for(int j = R[i]; j != i; j = R[j])
                    vis[Col[j]] = 0;
            }
        }
        return ret;
    }
    void Dance(int d) {
        if(d + f() >= ansd) return ;
        if(R[0] == 0) {
            if(d < ansd) ansd = d;
            return ;
        }
        int c = R[0];
        for(int i = R[0]; i != 0; i = R[i])
            if(S[i] < S[c]) c = i;
        for(int i = D[c]; i != c; i = D[i]) {
            remove(i);
            for(int j = R[i]; j != i; j = R[j]) remove(j);
            Dance(d + 1);
            for(int j = L[i]; j != i; j = L[j]) resume(j);
            resume(i);
        }
    }
};

DLX dlx;
int t, n, m, k;
double cx[N], cy[N], x[N], y[N];

bool judge(int a, int b, double radius) {
    ///a圆b点rad半径
    double dis = (x[a] - cx[b]) * (x[a] - cx[b]) + (y[a] - cy[b]) * (y[a] - cy[b]);
    if(dis <= radius * radius) return 1;
    else return 0;
}

bool check(double radius) {
    dlx.init(m, n);	///初始化
    for(int i = 1; i <= m; ++i)
        for(int j = 1; j <= n; ++j)
            if(judge(i, j, radius))
                dlx.Link(i, j);	///该位置为1
    dlx.ansd = inf;
    dlx.Dance(0);
    if(dlx.ansd <= k) return 1;
    else return 0;
}

double lower_bound(double l, double r) {
    while (r - l > eps) {
        double mid = (l + r) / 2.0;
        if (!check(mid)) l = mid;
        else r = mid;
    }
    return l;
}

int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; ++i) scanf("%lf%lf", &cx[i], &cy[i]);
        for(int i = 1; i <= m; ++i) scanf("%lf%lf", &x[i], &y[i]);
        printf("%.6f\n", lower_bound(0.0, 10000.0));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值