類型:2-sat
題目:http://acm.hdu.edu.cn/showproblem.php?pid=3622
來源:2010 Asia Regional Tianjin Site —— Online Contest
思路:二分枚舉半徑,判斷解是否存在
// hdoj 3622 Bomb Game
// wa wa 1984MS 612K
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)
const int N = 501;
const int M = 40010;
const int INF = 0x7f7f7f7f;
const double eps = 1e-8;
int low[N], dfn[N];
int belong[N];
bool inStack[N], vis[N];
stack<int> st;
int n, m, step, t;
int cnt1;
int head1[N], p[N][4];
double max_dis;
struct edge1 {
int v, nxt;
}E1[M];
void addedge1(int u, int v) {
E1[cnt1].v = v;
E1[cnt1].nxt = head1[u];
head1[u] = cnt1++;
}
double dis(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1- y2) * (y1 - y2));
}
void tarjan(int u) {
int i;
step++;
st.push(u);
low[u] = dfn[u] = step;
vis[u] = 1;
inStack[u] = 1;
for(i = head1[u]; i != -1; i = E1[i].nxt) {
int x = E1[i].v;
if(!vis[x]) {
tarjan(x);
low[u] = min(low[u], low[x]);
}
else if(inStack[x])
low[u]=min(low[u], dfn[x]);
}
if(low[u] == dfn[u]) {
t++;
while(1) {
int x = st.top();
st.pop();
belong[x] = t;
inStack[x] = 0;
if(x == u)
break;
}
}
}
bool is_ok(double dis, double x) {
if(x / 2.0 - dis >= eps)
return true;
return false;
}
void solve() {
int i, j;
double mid, l = 0, r = max_dis;
while(r - l > eps) {
mid = (l + r) / 2.0;
cnt1 = step = t = 0;
CLR(head1, -1);
CLR(vis, 0);
CLR(inStack, 0);
CLR(belong, 0);
FORE(i, 1, n) FORE(j, i + 1, n) {
if(!is_ok(mid, dis(p[i][0], p[i][1], p[j][0], p[j][1]))) {
addedge1(i, j + n);
addedge1(j, i + n);
}
if(!is_ok(mid, dis(p[i][0], p[i][1], p[j][2], p[j][3]))) {
addedge1(i, j);
addedge1(j + n, i + n);
}
if(!is_ok(mid, dis(p[i][2], p[i][3], p[j][0], p[j][1]))) {
addedge1(i + n, j + n);
addedge1(j, i);
}
if(!is_ok(mid, dis(p[i][2], p[i][3], p[j][2], p[j][3]))) {
addedge1(i + n, j);
addedge1(j + n, i);
}
}
while(!st.empty())
st.pop();
FORE(i, 1, 2 * n)
if(!vis[i])
tarjan(i);
bool sign = false;
FORE(i, 1, n)
if(belong[i] == belong[i + n]) {
sign = true;
break;
}
if(sign)
r = mid;
else
l = mid;
}
printf("%.2lf\n", l);
}
int main() {
int u, v, i, j;
while(scanf("%d", &n) == 1) {
max_dis = -INF;
FORE(i, 1, n) {
scanf("%d %d %d %d", &p[i][0], &p[i][1], &p[i][2], &p[i][3]);
max_dis = max(max_dis, dis(p[i][0], p[i][1], p[i][2], p[i][3]));
}
solve();
}
return 0;
}