题目:http://acm.hdu.edu.cn/showproblem.php?pid=3622
题意:
给n对炸弹可以放置的位置(每个位置为一个二维平面上的点),每次放置炸弹是时只能选择这一对中的其中一个点,每个炸弹爆炸的范围半径都一样,控制爆炸的半径使得所有的爆炸范围都不相交(可以相切),求解这个最大半径.
分析:
二分最大半径值r,然后2-sat构图判断其可行性.
对于两队位置(u,uu)和(v,vv),如果u和v之间的距离小于2*r,也就是说位置u和位置v处不能同时防止炸弹(两范围相交),那么这两个点不能同时选,非(A与B)==(非A)或(非B),然后套O(nm)的模板即可。
代码:
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 2000 + 10;
struct TwoSAT {
int n;
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2], c;
bool dfs(int x) {
if (mark[x^1]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = 0; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
}
void init(int n) {
this->n = n;
for (int i = 0; i < n*2; i++) G[i].clear();
memset(mark, 0, sizeof(mark));
}
// x = xval or y = yval
void add_clause(int x, int xval, int y, int yval) {
x = x * 2 + xval;
y = y * 2 + yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve() {
for(int i = 0; i < n*2; i += 2)
if(!mark[i] && !mark[i+1]) {
c = 0;
if(!dfs(i)) {
while(c > 0) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
return true;
}
};
///////// 题目相关
#include<algorithm>
#include<cmath>
typedef pair<int,int>pii;
#define x first
#define y second
TwoSAT solver;
int n;
pii s[maxn][2];
double dis(pii a,pii b) {
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
}
bool test(double diff) {
solver.init(n);
for(int i = 0; i < n; i++) for(int a = 0; a < 2; a++)
for(int j = i+1; j < n; j++) for(int b = 0; b < 2; b++)
if(dis(s[i][a],s[j][b]) < diff*2) solver.add_clause(i, a^1, j, b^1);
//如果两个点距离小于直径,那么两个点不能同时选
return solver.solve();
}
int main() {
//freopen("f.txt","r",stdin);
while(scanf("%d", &n) == 1 && n) {
for(int i=0; i<n; i++)
for(int j=0; j<2; j++)scanf("%d%d",&s[i][j].x,&s[i][j].y);
double L = 0, R = 1e5;
for(int i=0; i<50; i++) {
double M = L + (R-L+1)/2;
if(test(M)) L = M;
else R = M-1;
}
printf("%.2lf\n", L);
}
return 0;
}