Problem
acm.hdu.edu.cn/showproblem.php?pid=2295
vjudge.net/contest/65998#problem/C
Reference
www.cnblogs.com/jh818012/p/3252154.html
Meaning
m 个雷达有相同的半径 r,要使所有 n 个城市都被至少一个雷达覆盖,但所用雷达数不能超过 k 个,求满足条件的最小半径 r。
Analysis
重复覆盖的模板题。
重复覆盖与精确覆盖的不同点主要在于:每一列都可以有多个 1(同一城市可以多个雷达覆盖),导致在删行、删列时的操作不同。
(对比精确覆盖模板:blog.csdn.net/hackertom/article/details/62892288)
1. 精确覆盖一列只能有一个 1,所以在考虑某一列时,要在此列有 1 的所有行全部删掉,从这些行中选一行作为此次被选中的行,选中行中有 1 的列不能再考虑,而且在对应选中有 1 的列中有 1 的那些行也要全部删掉;
但重复覆盖允许一列由多个 1,所以考虑某一列时,也是选其中一行,但只删被选中的那一行,选中行中有 1 的列不需再考虑所以删列,在对应选中有 1 的列中有 1 的那些行全都不能删。
2. 精确覆盖删列的方式是:在列头哨兵那里改下 left 和 right 的标记就算是删掉整列了;而多重覆盖的删列:整列的所有元素都要处理 left 和 right 标记。
精确覆盖删行:对这行的所有元素都要改 up 和 down 的标记;而多重覆盖的删行:不改 up 和 down 标记,但对这行所有的元素都进行(多重覆盖式的)删列,因为这行所有 1 所在列都被删掉,所以没有列可以访问这一行,所以相当于已经删行。
3. 多重覆盖删行删得少,要加个估价函数来剪枝。估价函数就是:用精确覆盖来计算目前至少仍需添加多少行才能完成覆盖。
Code
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int M = 50, N = 50, K = 50, P = M*N + N + 1;
const double EPS = 1e-8;
struct point
{
int x, y;
} radar[M+1], city[N+1];
int row[P], col[P], head[M+1], sum[N+1];
int up[P], down[P], left[P], right[P];
bool vis[N+1];
inline double dis(const point &a, const point &b)
{
return sqrt((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));
}
void init(int c)
{
for(int i=0; i<=c; ++i)
{
row[i] = sum[i] = 0;
col[i] = up[i] = down[i] = i;
left[i] = i - 1;
right[i] = i + 1;
}
left[0] = c;
right[c] = 0;
memset(head, -1, sizeof head);
}
void link(int r, int c, int id)
{
row[id] = r;
col[id] = c;
++sum[c];
up[id] = up[c];
down[id] = c;
up[c] = down[up[c]] = id;
if(head[r] == -1)
head[r] = left[id] = right[id] = id;
else
{
left[id] = left[head[r]];
right[id] = head[r];
left[head[r]] = right[left[head[r]]] = id;
}
}
int evaluate()
{
memset(vis, false, sizeof vis);
int cnt = 0;
for(int i=right[0]; i; i=right[i])
if(!vis[i])
{
++cnt;
vis[i] = true;
// 精确覆盖的方式
for(int j=down[i]; j!=i; j=down[j])
for(int k=right[j]; k!=j; k=right[k])
vis[col[k]] = true;
}
return cnt;
}
void remove(int c)
{
for(int i=down[c]; i!=c; i=down[i])
{
left[right[i]] = left[i];
right[left[i]] = right[i];
}
}
void resume(int c)
{
for(int i=up[c]; i!=c; i=up[i])
left[right[i]] = right[left[i]] = i;
}
int dance(int now, int k)
{
if(now + evaluate() > k)
return -1;
if(!right[0])
return now > k ? -1 : now;
int c = right[0];
for(int i=right[c]; i; i=right[i])
if(sum[i] < sum[c])
c = i;
for(int i=down[c], tmp; i!=c; i=down[i])
{
remove(i); // 这句在循环内
for(int j=right[i]; j!=i; j=right[j])
remove(j); // 不是col[j]
if(~(tmp = dance(now+1, k)))
return tmp;
for(int j=left[i]; j!=i; j=left[j])
resume(j);
resume(i); // 这句也在循环内
}
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for(int i=1; i<=n; ++i)
scanf("%d%d", &city[i].x, &city[i].y);
for(int j=1; j<=m; ++j)
scanf("%d%d", &radar[j].x, &radar[j].y);
double low = 0.0, high = 1500.0;
while(high - low > EPS)
{
double r = (low + high) / 2.0;
init(n);
for(int i=1, id=n; i<=m; ++i)
for(int j=1; j<=n; ++j)
if(dis(radar[i], city[j]) <= r)
link(i, j, ++id);
if(~dance(0, k))
high = r - EPS;
else
low = r + EPS;
}
printf("%.6f\n", high);
}
return 0;
}