HDU 3622 Bomb Game

Problem Description

  Robbie is playing an interesting computer game. The game field is an unbounded 2-dimensional region. There are N rounds in the game. At each round, the computer will give Robbie two places, and Robbie should choose one of them to put a bomb. The explosion area of the bomb is a circle whose center is just the chosen place. Robbie can control the power of the bomb, that is, he can control the radius of each circle. A strange requirement is that there should be no common area for any two circles. The final score is the minimum radius of all the N circles.
Robbie has cracked the game, and he has known all the candidate places of each round before the game starts. Now he wants to know the maximum score he can get with the optimal strategy.

Input

  The first line of each test case is an integer N (2 <= N <= 100), indicating the number of rounds. Then N lines follow. The i-th line contains four integers x1i, y1i, x2i, y2i, indicating that the coordinates of the two candidate places of the i-th round are (x1i, y1i) and (x2i, y2i). All the coordinates are in the range [-10000, 10000].

Output

  Output one float number for each test case, indicating the best possible score. The result should be rounded to two decimal places.

Sample Input

2
1 1 1 -1
-1 -1 -1 1
2
1 1 -1 -1
1 -1 -1 1

Sample Output

1.41
1.00

Solution

题目大意:每次给你平面上两个点,这两个点你只能选其中之一。然后要求以你选出的这2N个点为圆心画一样大圆,圆不能相交,可以相切,问圆的半径最大是多少。

经典的2-SAT模型,今天学到2-SAT,就顺便把这道题做了。我们二分答案mid,按照题目中给的关系建立2-SAT模型,跑一边强连通分量,判断2-SAT有无解即可。

Code

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1<<30;
int getint() {
    int r = 0, k = 1; char c = getchar();
    for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
    for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
    return r * k;
}
struct Interesting {
	int to, next;
} edge[2000005];
int h[5005], cnte, tot;
void ins(int x, int y) {
	edge[++cnte].to = y;
	edge[cnte].next = h[x];
	h[x] = cnte;
}
int dfs_clock = 0, sta[5005], tail, cnt, maxnode;
int dfn[5005], low[5005], bel[5005];
bool insta[5005];
void init() {
	for (int i = 0; i <= maxnode; ++i) {
		h[i] = 0;
		dfn[i] = 0;
		bel[i] = 0;
	}
	cnte = 1; dfs_clock = 0; tail = 0; cnt = 0;
}
void Tarjan(int now) {
	dfn[now] = low[now] = (++dfs_clock);
	insta[now] = true; sta[++tail] = now;
	for (int i = h[now]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (!dfn[v]) {
			Tarjan(v);
			low[now] = min(low[v], low[now]);
		} else {
			if (insta[v]) low[now] = min(low[now], dfn[v]);
		}
	}
	int cur;
	if (dfn[now] == low[now]) {
		++cnt;
		while (true) {
			cur = sta[tail--];
			bel[cur] = cnt;
			insta[cur] = false;
			if (cur == now) break;
		}
	}
}
int x[5005], y[5005], n;
inline double fang(double x) { return x * x; }
inline double getDis(int i, int j) { return sqrt(fang(x[i] - x[j]) + fang(y[i] - y[j])); }
void GetSCC() {
	for (int i = 2; i < maxnode; ++i) {
		if (!dfn[i]) Tarjan(i);
	}
}
bool check(double x) {
	init();
	int id1, id2, id1n, id2n;
	for (int i = 1; i <= n; ++i) {
		id1 = i << 1;
		id2 = id1 | 1;
		id1n = id1 + (n << 1);
		id2n = id2 + (n << 1);
		ins(id1n, id2);
		ins(id2n, id1);
		ins(id1, id2n);
		ins(id2, id1n);
	}
	double dis;
	for (int i = 2; i <= tot; ++i)
		for (int j = 2; j <= tot; ++j) {
			if (i == j) continue;
			dis = getDis(i, j);
			if (dis < x * 2) {
				ins(i, j + (n << 1));
				ins(j, i + (n << 1));
			}
		}
	GetSCC();
	for (int i = 2; i <= tot; ++i)
		if (bel[i] == bel[i + (n << 1)])
			return false;
	return true;
}
int main() {
	while (scanf("%d", &n) == 1) {
		tot = 1;
		for (int i = 1; i <= n; ++i) {
			x[++tot] = getint(); y[tot] = getint();
			x[++tot] = getint(); y[tot] = getint();
		}
		maxnode = tot << 1;
		double L = 0, R = 30000, mid;
		for (int i = 0; i < 50; ++i) {
			mid = (L + R) / 2;
			if (check(mid)) L = mid;
			else R = mid;
		}
		printf("%.2lf\n", L);
	}
    return 0;
}

从kuangbin博客上看到两遍DFS实现的强连通分量,感觉好高端,拿来分享一下~

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1<<30;
int getint() {
    int r = 0, k = 1; char c = getchar();
    for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
    for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
    return r * k;
}
struct Interesting {
	int to, next;
} edge1[1000005], edge2[1000005];
int h1[5005], h2[5005], cnte1, cnte2, tot, maxnode;
int dfs_clock = 0, cnt;
int bel[5005];
int x[5005], y[5005], T[5005], ptr;
bool vis1[5005], vis2[5005];
void ins(int x, int y) {
	edge1[++cnte1].to = y;
	edge1[cnte1].next = h1[x];
	h1[x] = cnte1;
	edge2[++cnte2].to = x;
	edge2[cnte2].next = h2[y];
	h2[y] = cnte2;
}
double fang(double x) {
	return x * x;
}
double getDis(int i, int j) {
	return sqrt(fang(x[i] - x[j]) + fang(y[i] - y[j]));
}
void dfs1(int now) {
	vis1[now] = true;
	for (int i = h1[now]; i; i = edge1[i].next)
		if (!vis1[edge1[i].to])
			dfs1(edge1[i].to);
	T[++ptr] = now;
}
void dfs2(int now) {
	bel[now] = cnt;
	vis2[now] = true;
	for (int i = h2[now]; i; i = edge2[i].next)
		if (!vis2[edge2[i].to])
			dfs2(edge2[i].to);
}
void GetSCC() {
	for (int i = 2; i < maxnode; ++i) {
		if (!vis1[i]) dfs1(i);
	}
	for (int i = ptr; i; --i) {
		if (!vis2[T[i]]) {
			++cnt;
			dfs2(T[i]);
		}
	}
}
void init() {
	for (int i = 0; i <= maxnode; ++i) {
		h1[i] = h2[i] = 0;
		bel[i] = 0;
		vis1[i] = vis2[i] = false;
	}
	cnte1 = 1; dfs_clock = 0; ptr = 0; cnt = 0; cnte2 = 1;
}
int n;
bool check(double x) {
	init();
	int id1, id2, id1n, id2n;
	for (int i = 1; i <= n; ++i) {
		id1 = i << 1;
		id2 = id1 | 1;
		id1n = id1 + (n << 1);
		id2n = id2 + (n << 1);
		ins(id1n, id2);
		ins(id2n, id1);
		ins(id1, id2n);
		ins(id2, id1n);
	}
	double dis;
	for (int i = 2; i <= tot; ++i)
		for (int j = 2; j <= tot; ++j) {
			if (i == j) continue;
			dis = getDis(i, j);
			if (dis < x * 2) {
				ins(i, j + (n << 1));
				ins(j, i + (n << 1));
			}
		}
	GetSCC();
	for (int i = 2; i <= tot; ++i)
		if (bel[i] == bel[i + (n << 1)])
			return false;
	return true;
}
int main() {
	while (scanf("%d", &n) == 1) {
		tot = 1;
		for (int i = 1; i <= n; ++i) {
			x[++tot] = getint(); y[tot] = getint();
			x[++tot] = getint(); y[tot] = getint();
		}
		maxnode = tot << 1; 
		double L = 0, R = 30000, mid;
		for (int i = 0; i < 50; ++i) {
			mid = (L + R) / 2;
			if (check(mid)) L = mid;
			else R = mid;
		}
		printf("%.2lf\n", L);
	}
    return 0;
}

 

Show Comments