HDU 3622 二分+2-SAT
Bomb Game
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3033 Accepted Submission(s): 1023
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.
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
题意:放置n对炸弹,每对炸弹只放置一个,每个炸弹的爆炸半径一样,求所有n个炸弹爆炸范围不相交时最大半径。
解题思路:将半径进行二分,根据冲突关系建图,tarjin缩点进行判定可行性。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
using namespace std;
const double eps=1e-6;
const int maxn=500;
const int maxm=1000000;
struct point
{
double x,y;
}p[maxn];
double dis(point a,point b)
{
double ss=a.x-b.x;
double tt=a.y-b.y;
return sqrt(ss*ss+tt*tt);
}
int head[maxn],tol,low[maxn],dfn[maxn],Stack[maxn],instack[maxn],belong[maxn],scc,top,indexx,n;
struct node
{
int next,to;
}edge[maxm];
void add(int u,int v)
{
edge[tol].next=head[u];
edge[tol].to=v;
head[u]=tol++;
}
void tarjin(int u)
{
low[u]=dfn[u]=++indexx;
Stack[top++]=u;instack[u]=1;
int i,v;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v])
{
tarjin(v);
if(low[u]>low[v])low[u]=low[v];
}
else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=0;
belong[v]=scc;
}while(u!=v);
}
}
int solve(double r)
{
memset(head,-1,sizeof(head));tol=0;
int i,j,u,v;
for(i=0;i<2*n;i++)
{
int pp;
if(i%2==0)pp=i+2;
else pp=i+1;
for(j=pp;j<2*n;j++)
if(dis(p[i],p[j])+eps<2*r)
{
add(i,j^1);
add(j,i^1);
}
}
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
scc=top=indexx=0;
for(i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
for(i=0;i<n;i++)
if(belong[2*i]==belong[2*i+1])return 0;
return 1;
}
int main()
{
int i,j,k,m;
while(~scanf("%d",&n))
{
for(i=0;i<2*n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
double left=0,right=1000000.0,mid;
while(right-left>eps)
{
mid=(left+right)/2.0;
i=solve(mid);
if(i==0)right=mid;
else left=mid;
}
printf("%.2lf\n",left);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
using namespace std;
const double eps=1e-6;
const int maxn=500;
const int maxm=1000000;
struct point
{
double x,y;
}p[maxn];
double dis(point a,point b)
{
double ss=a.x-b.x;
double tt=a.y-b.y;
return sqrt(ss*ss+tt*tt);
}
int head[maxn],tol,low[maxn],dfn[maxn],Stack[maxn],instack[maxn],belong[maxn],scc,top,indexx,n;
struct node
{
int next,to;
}edge[maxm];
void add(int u,int v)
{
edge[tol].next=head[u];
edge[tol].to=v;
head[u]=tol++;
}
void tarjin(int u)
{
low[u]=dfn[u]=++indexx;
Stack[top++]=u;instack[u]=1;
int i,v;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v])
{
tarjin(v);
if(low[u]>low[v])low[u]=low[v];
}
else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=0;
belong[v]=scc;
}while(u!=v);
}
}
int solve(double r)
{
memset(head,-1,sizeof(head));tol=0;
int i,j,u,v;
for(i=0;i<2*n;i++)
{
int pp;
if(i%2==0)pp=i+2;
else pp=i+1;
for(j=pp;j<2*n;j++)
if(dis(p[i],p[j])+eps<2*r)
{
add(i,j^1);
add(j,i^1);
}
}
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
scc=top=indexx=0;
for(i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
for(i=0;i<n;i++)
if(belong[2*i]==belong[2*i+1])return 0;
return 1;
}
int main()
{
int i,j,k,m;
while(~scanf("%d",&n))
{
for(i=0;i<2*n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
double left=0,right=1000000.0,mid;
while(right-left>eps)
{
mid=(left+right)/2.0;
i=solve(mid);
if(i==0)right=mid;
else left=mid;
}
printf("%.2lf\n",left);
}
return 0;
}

浙公网安备 33010602011771号