题意:给出n对点,每对点只能选取一个。每个点都会以自己为中心进行爆炸且半径相同,问最大爆炸半径是多少。
解法:两个之中选一个,可以考虑2-SAT求解。由于半径是要求的,所以二分半径,然后每次都建图用2-SAT判断看是否可行。
2-SAT建图中的边代表的是,如果选取i,就一定要选取j。对于这道题,如果有两个点的距离小于爆炸距离,即不符合,那么认为选取了i 只能选取 j^1。选取了j只能选取 i^1。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1005 * 2;
const int maxm = 1e6 + 5;
const double eps = 1e-4;
stack <int> sta;
int dfn[maxn], low[maxn], key[maxn], tot = 0, num = 1;
int belong[maxn];
int n, m;
int a[maxn], b[maxn];
struct Node {
int x1, x2, y1, y2;
}boom[maxn];
int to[maxm], nx[maxm], head[maxn], ppp;
void add_edge(int u, int v) {
to[ppp] = v, nx[ppp] = head[u], head[u] = ppp++;
}
void tarjan(int u) {
sta.push(u);
dfn[u] = low[u] = tot++;
key[u] = 1;
for(int i = head[u]; ~i; i = nx[i]) {
int v = to[i];
if(dfn[v] == -1) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(key[v] == 1) {
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]) {
while(1) {
int v = sta.top();
sta.pop();
belong[v] = num;
key[v] = 2;
if(v == u)
break;
}
num++;
}
}
double Count(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void build(double mid) {
mid *= 2.0;
for(int i = 1, a, b; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
double tmp1 = Count(boom[i].x1, boom[i].y1, boom[j].x1, boom[j].y1);
double tmp2 = Count(boom[i].x1, boom[i].y1, boom[j].x2, boom[j].y2);
double tmp3 = Count(boom[i].x2, boom[i].y2, boom[j].x1, boom[j].y1);
double tmp4 = Count(boom[i].x2, boom[i].y2, boom[j].x2, boom[j].y2);
if(tmp1 < mid) {
a = i * 2, b = j * 2;
add_edge(a, b ^ 1);
add_edge(b, a ^ 1);
}
if(tmp4 < mid) {
a = i * 2 + 1, b = j * 2 + 1;
add_edge(a, b ^ 1);
add_edge(b, a ^ 1);
}
if(tmp2 < mid) {
a = i * 2, b = j * 2 + 1;
add_edge(a, b ^ 1);
add_edge(b, a ^ 1);
}
if(tmp3 < mid) {
a = i * 2 + 1, b = j * 2;
add_edge(a, b ^ 1);
add_edge(b, a ^ 1);
}
}
}
}
bool Check() {
for(int i = 1; i <= n; i++)
if(belong[2 * i] == belong[(2 * i) ^ 1])
return 0;
return 1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
while(scanf("%d", &n) != EOF) {
for(int i = 1; i <= n; i++) {
scanf("%d%d%d%d", &boom[i].x1, &boom[i].y1, &boom[i].x2, &boom[i].y2);
}
double front = 0, back = 1e9+7;
while(back - front > eps) {
// cout << front << ' ' << back << '\n';
memset(head, -1, sizeof(head));
memset(dfn, -1, sizeof(dfn));
memset(key, 0, sizeof(key));
ppp = tot = 0;
num = 1;
double mid = (front + back) / 2.0;
build(mid);
for(int i = 2; i <= 2 * n + 1; i++) {
if(dfn[i] == -1)
tarjan(i);
}
if(Check())
front = mid;
else
back = mid;
}
printf("%.2f\n", front);
}
return 0;
}

本文介绍了一种使用2-SAT算法解决给定若干对点中选取一个进行爆炸并求最大爆炸半径的问题。通过二分查找半径,并利用2-SAT判断在特定半径下方案的可行性。
457

被折叠的 条评论
为什么被折叠?



