HDU 2295 Radar

Radar

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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个操作员,然后问你在k个操作员操控雷达的情况下当r为多少的时候可以将所有的点包含在雷达里面。

思路:

当我们求r的时候显然是用二分来做的,然后我们就可以用重复覆盖了,k个人在m个雷达中如何选择最少雷达来达到r最小化这个是重复覆盖的问题。

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 60;
const double eps = 1e-8;
const int maxnode = maxn * maxn + 10;
int k;
struct DLX {
    int n;
    int m;
    int size;
    int u[maxnode];
    int d[maxnode];
    int l[maxnode];
    int r[maxnode];
    int col[maxnode];
    int row[maxnode];
    int h[maxn];
    int s[maxn];
    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;
        }
        l[0] = m;
        r[m] = 0;
        size = m;
        memset(h, -1, sizeof(h));
    }
    void link(int ro, int c) {
        size++;
        s[c]++;
        col[size] = c;
        row[size] = ro;
        d[size] = d[c];
        u[d[c]] = size;
        u[size] = c;
        d[c] = size;
        if (h[ro] < 0) h[ro] = l[size] = r[size] = size;
        else {
            r[size] = r[h[ro]];
            l[r[h[ro]]] = size;
            l[size] = h[ro];
            r[h[ro]] = size;
        }
    }
    void removen(int c) {
        for (int i = d[c]; i != c; i = d[i]) {
            l[r[i]] = l[i];
            r[l[i]] = r[i];
        }
    }
    void resumen(int c) {
        for (int i = u[c]; i != c; i = u[i]) {
            l[r[i]] = i;
            r[l[i]] = i;
        }
    }
    bool flag[maxnode];
    int f() {
        int sum = 0;
        for (int i = r[0]; i != 0; i = r[i]) flag[i] = true;
        for (int i = r[0]; i != 0; i = r[i]) {
            if (flag[i]) {
                sum++;
                flag[i] = false;
                for (int j = d[i]; j != i; j = d[j]) {
                    for (int k = r[j]; k != j; k = r[k]) {
                        flag[col[k]] = false;
                    }
                }
            }
        }
        return sum;
    }
    bool dance(int step) {
        if (step + f() > k) return false;
        if (r[0] == 0) return step <= k;
        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]) {
            removen(i);
            for (int j = r[i];j != i;j = r[j]) removen(j);
            if (dance(step+1)) return true;
            for (int j = l[i];j != i;j = l[j]) resumen(j);
            resumen(i);
        }
        return false;
    }
};
DLX dlx;
struct Point {
    double x;
    double y;
};
Point a[maxn], b[maxn];
double getdis(Point a, Point b) {
    double L1 = b.x - a.x;
    double L2 = b.y - a.y;
    return sqrt(L1 * L1 + L2 * L2);
}
int check(double mid, int n, int m) {
    dlx.init(m, n);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (getdis(b[i], a[j]) <= mid) dlx.link(i + 1, j + 1); //这个地方需要+1,因为行列是从1开始的,0是终点,不能作为行和列的头的编号
        }
    }
    return dlx.dance(0);
}
int main() {
    int n, m, t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d %d", &n, &m, &k);
        for (int i = 0; i < n; i++) scanf("%lf %lf", &a[i].x, &a[i].y);
        for (int i = 0; i < m; i++) scanf("%lf %lf", &b[i].x, &b[i].y);
        double l = 0, r = 1e8;
        while (r - l > eps) {
            double mid = (l + r) / 2;
            if (check(mid, n, m)) r = mid;
            else l = mid;
        }
        printf("%.6lf\n", r);
    }
    return 0;
}

你可能感兴趣的:(HDU)