Link
Description
平面上有 n n n 个好圆, m m m 个坏点。要求选出一个半径不超过 R R R 的圆,使得它不和任何好圆有交。求该圆包含的最多坏点个数。
Solution
本题并没有完全正确的多项式解法。我们考虑使用模拟退火的技巧。
如果你不会模拟退火,请参考 S iyuan \color{black}{\text S}\color{red}{\text {iyuan}} Siyuan 的博客。
对于每次 solve \text {solve} solve,先根据半径随机出一个初始点。对于每次退火,还是根据半径将当前点移动一定距离,然后按照模拟退火的算法判断是否要将当前点移动即可。值得注意的是,由于答案较小,所以我们在计算 exp \text{exp} exp 时需要乘一较大的常数(比如 10000 10000 10000)。
Code
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 15;
const int maxm = 1005;
int N, M;
double R, x[maxn], y[maxn], r[maxn], p[maxm], q[maxm];
double rand01() {
return rand() % 10001 / 10000.;
}
void randPos(double &x, double &y) {
x = 2 * R * rand01() - R;
y = 2 * R * rand01() - R;
}
double sqr(double x) {
return x * x;
}
double dist(double ax, double ay, double bx, double by) {
return sqrt(sqr(ax - bx) + sqr(ay - by));
}
int solve(double a, double b) {
double res = R;
for (int i = 1; i <= N; i++) {
res = min(res, dist(a, b, x[i], y[i]) - r[i]);
}
if (res < 0) return 0;
int ans = 0;
for (int i = 1; i <= M; i++) {
ans += dist(a, b, p[i], q[i]) <= res;
}
return ans;
}
int simulateAnneal(double x, double y) {
double T = R;
int res, cur = solve(x, y), ans = cur;
while (T > 1e-2) {
double nx = x + 2 * T * rand01() - T;
double ny = y + 2 * T * rand01() - T;
res = solve(nx, ny);
if (res > cur || exp(10000 * (res - cur) / T) > rand01()) {
cur = res;
x = nx, y = ny;
}
ans = max(ans, cur);
T -= T / 500;
}
return ans;
}
int main() {
srand(time(0));
scanf("%d %d %lf", &N, &M, &R);
for (int i = 1; i <= N; i++) {
scanf("%lf %lf %lf", &x[i], &y[i], &r[i]);
}
for (int i = 1; i <= M; i++) {
scanf("%lf %lf", &p[i], &q[i]);
}
int ans = 0;
double x, y;
for (int i = 1; i <= 20; i++) {
randPos(x, y);
ans = max(ans, simulateAnneal(x, y));
}
printf("%d\n", ans);
return 0;
}