HDU 3622 Bomb Game 2-sat 模板题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/83055007

                                           HDU 3622 Bomb Game

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 x 1i, y 1i, x 2i, y 2i, indicating that the coordinates of the two candidate places of the i-th round are (x 1i, y 1i) and (x 2i, y 2i). 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

题意:

给你n对点的坐标,每对只能选一个,所有的点之间的距离不能超过r,求可行方案中半径最大的一个

题解:

二分半径 + 2-sat模板

#include <bits/stdc++.h>

using namespace std;
const int maxn = 500;
const int INF = 0x3f3f3f3f;
#define eps 1e-4
int x[maxn],y[maxn];
int n;

vector <int> G[maxn];
vector <int> rG[maxn];
vector <int> vs;
bool used[maxn];
int cmp[maxn];

void dfs(int x)
{
  used[x] = true;
  for(int i = 0; i < G[x].size(); i++)
    if(!used[G[x][i]])  dfs(G[x][i]);
  vs.push_back(x);
}

void rdfs(int x, int k)
{
  used[x] = true;
  for(int i = 0; i < rG[x].size(); i++)
    if(!used[rG[x][i]])  rdfs(rG[x][i],k);
  cmp[x] = k;
}

int scc()
{
  vs.clear();
  memset(used,false,sizeof(used));
  for(int i = 0; i < 2*n; i++)
    if(!used[i])  dfs(i);
  int k = 0;
  memset(used,false,sizeof(used));
  for(int i = vs.size()-1; i >= 0; i--)
    if(!used[vs[i]]) rdfs(vs[i],++k);
  return k;
}

int dis(int i, int j)
{
  return (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
}

void add_edge(int u, int v)
{
  G[u].push_back(v);
  rG[v].push_back(u);
}

bool check(double mid)
{
  for(int i = 0; i < 2*n; i++) {G[i].clear();rG[i].clear();}
  memset(cmp,-1,sizeof(cmp));
  for(int i = 0; i < n; i++)
    for(int j = i+1; j < n; j++)
     {
       int u1 = i*2,u2 = u1+1,v1 = j*2,v2 = v1+1;
       bool t1 = dis(u1,v1) < (4*mid*mid);
       bool t2 = dis(u1,v2) < (4*mid*mid);
       bool t3 = dis(u2,v1) < (4*mid*mid);
       bool t4 = dis(u2,v2) < (4*mid*mid);
       if(t1)  {add_edge(u1,v2);add_edge(v1,u2);}
       if(t2)  {add_edge(u1,v1);add_edge(v2,u2);}
       //if(t1&&t2) return false;
       if(t3)  {add_edge(u2,v2);add_edge(v1,u1);}
       if(t4)  {add_edge(u2,v1);add_edge(v2,u1);}
       //if(t3&&t4) return false;
     }
  scc();
  for(int i = 0; i < n; i++)
     if(cmp[i*2] == cmp[i*2+1]) return false;
  return true;
}

int main()
{
  while(~scanf("%d",&n))
  {
  for(int i = 0; i < n; i++)
    scanf("%d%d%d%d",&x[i*2],&y[i*2],&x[i*2+1],&y[i*2+1]);
  double st = 0,ed = 20000;
  while(ed - st > eps)
  {
    double mid = st+(ed-st)/2;
    if(check(mid))  st = mid;
    else            ed = mid;
  }
  printf("%.2f\n",st);
}
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/83055007
今日推荐