题目:
http://acm.hdu.edu.cn/showproblem.php?pid=2295
题意:
有n个城市在二维平面上,还有m个地点,从这m个地点中选出不超过k个放置雷达,雷达有一定的覆盖半径,问半径最小为多少时雷达可以覆盖所有城市
思路:
二分枚举半径,用DLX重复覆盖判断能否选出不超过k个雷达覆盖所有城市
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int X = 10000 + 10, N = 50 + 10, M = 50 + 10, INF = 0x3f3f3f3f;
double ax[M], ay[M], bx[N], by[N];
struct DLX
{
int U[X], D[X], L[X], R[X], row[X], col[X];
int H[N], S[M];
int head, sz, tot, n, m;
bool vis[M];
void init(int _n, int _m)
{
n = _n, m = _m;
for(int i = 0; i <= m; i++)
L[i] = i-1, R[i] = i+1, U[i] = D[i] = i, S[i] = 0;
head = 0, tot = 0, sz = m;
L[head] = m, R[m] = head;
for(int i = 1; i <= n; i++) H[i] = -1;
}
void link(int r, int c)
{
++S[col[++sz]=c];
row[sz] = r;
D[sz] = D[c], U[D[c]] = sz;
U[sz] = c, D[c] = sz;
if(H[r] < 0) H[r] = L[sz] = R[sz] = sz;
else R[sz] = R[H[r]], L[R[H[r]]] = sz, L[sz] = H[r], R[H[r]] = sz;
}
void del(int x)
{
for(int i = D[x]; i != x; i = D[i])
R[L[i]] = R[i], L[R[i]] = L[i];
}
void recover(int x)
{
for(int i = U[x]; i != x; i = U[i])
R[L[i]] = L[R[i]] = i;
}
int fun_f()
{
memset(vis, 0, sizeof vis);
int num = 0;
for(int i = R[head]; i != head; i = R[i])
if(! vis[i])
{
vis[i] = true;
num++;
for(int j = D[i]; j != i; j = D[j])
for(int k = R[j]; k != j; k = R[k])
vis[col[k]] = true;
}
return num;
}
bool dance(int dep, int k)
{
if(R[head] == head)
{
if(dep-1 <= k) return true;
else return false;
}
if(dep-1 + fun_f() > k) return false;//注意不要带=啊,因为这个wa了一下
int c = R[head];
for(int i = R[head]; i != head; i = R[i])
if(S[i] < S[c]) c = i;
for(int i = D[c]; i != c; i = D[i])
{
del(i);
for(int j = R[i]; j != i; j = R[j]) del(j);
if(dance(dep + 1, k)) return true;
for(int j = L[i]; j != i; j = L[j]) recover(j);
recover(i);
}
return false;
}
}dlx;
int main()
{
int t, n, m, _k;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &m, &_k);
for(int i = 1; i <= n; i++) scanf("%lf%lf", &ax[i], &ay[i]);
for(int i = 1; i <= m; i++) scanf("%lf%lf", &bx[i], &by[i]);
double l = 0.0, r = 10000.0, radius;
for(int i = 1; i <= 50; i++)
{
double mid = (l + r) / 2.0;
dlx.init(m, n);
for(int j = 1; j <= m; j++)
for(int k = 1; k <= n; k++)
if((bx[j]-ax[k]) * (bx[j]-ax[k]) + (by[j]-ay[k]) * (by[j]-ay[k]) <= mid*mid)
dlx.link(j, k);
if(dlx.dance(1, _k)) radius = mid, r = mid;
else l = mid;
}
printf("%.6f\n", radius);
}
return 0;
}