hdu 3622 Bomb Game【2-SAT-------Tarjan强连通】

来源:互联网 发布:社交网络中文 编辑:程序博客网 时间:2024/05/19 19:16

Bomb Game

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4949    Accepted Submission(s): 1769

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

Source

2010 Asia Regional Tianjin Site —— Online Contest

 

题目大意:一共给你n个区域,每个区域有两个点,每个区域都必须选择一个点放置炸弹,让你找一个最大爆炸半径,使得每个区域中的点与点之间不会产生连锁爆炸反应(两个同时爆炸就算是连锁反应。)。


思路:


1、n个区域,每个区域有两个点,只能选择一个点放置炸弹,明显的2-sat标志。确定使用Tarjan算法。


2、最大半径的枚举会导致超时,我们二分最大半径。确定使用二分查找算法。


3、对于每一个枚举到的最大半径,矛盾边就是两个区域中某个点(a)和某个点(b),如果其距离小于等于了最大半径,那么这两个炸弹就会产生连锁爆炸的反应,那么(a,b)这条边就是矛盾边,也就是说我们需要建(a,!b)和(b,!a)这两条边。


4、然后对于每一次建好的图,Tarjan求一下强连通,看看同一区域中的两个点是否矛盾即可



Ac代码:


#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>using namespace std;#define eps 1e-6int head[100000];struct EdgeNode{    int to;    int w;    int next;}e[1000000];struct point{    int x,y;}a[1000][2];int vis[100000];int dfn[100000];int low[100000];int stack[100000];int color[100000];int cont,n,sig,tt,cnt;double dis(point a,point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void Tarjan(int u){    vis[u]=1;    dfn[u]=low[u]=cnt++;    stack[++tt]=u;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        if(vis[v]==0)Tarjan(v);        if(vis[v]==1)low[u]=min(low[u],low[v]);    }    if(dfn[u]==low[u])    {        sig++;        do        {            color[stack[tt]]=sig;            vis[stack[tt]]=-1;        }        while(stack[tt--]!=u);    }}void add(int from,int to){    e[cont].to=to;    e[cont].next=head[from];    head[from]=cont++;}int Slove(double mid){    //printf("%lf\n",mid);    cont=0;    cnt=1;    tt=-1;    sig=0;    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    memset(color,0,sizeof(color));    memset(vis,0,sizeof(vis));    memset(stack,0,sizeof(stack));    memset(head,-1,sizeof(head));    for(int i=0;i<n;i++)    {        for(int j=i+1;j<n;j++)        {            for(int k=0;k<2;k++)            {                for(int l=0;l<2;l++)                {                    if(dis(a[i][k],a[j][l])<=mid)                    {                        add(i*2+k+1,j*2+3-(l+1));                        add(j*2+l+1,i*2+3-(k+1));                    }                }            }        }    }    for(int i=1;i<=2*n;i++)    {        if(vis[i]==0)        Tarjan(i);    }    for(int i=1;i<=2*n;i+=2)    {        if(color[i]==color[i+1])return 0;    }    return 1;}void erfen(){    double l=0;    double r=sqrt(2)*200000;    double mid;    while(r-l>=eps)    {        mid=(l+r)/2;        if(Slove(mid)==1)        {            l=mid;        }        else        {            r=mid;        }    }    printf("%.2f\n",mid/2);}int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            scanf("%d%d%d%d",&a[i][0].x,&a[i][0].y,&a[i][1].x,&a[i][1].y);        }        erfen();    }}




0 0