zoj 3717 2-sat

http://vjudge.net/contest/view.action?cid=48609#problem/D

Description

The weather is wonderful today. Gao takes a walk in the garden with his girlfriend. His girlfriend likes balloons so much, so that she wants to fly some balloons (not kites!) in the garden.

We can regard the garden as a three-dimensional space, and its coordinate starts from (0,0,0) to (10000,10000,10000). There are Ngroups of balloons, each of groups has a red balloon and a blue balloon. We can regard each balloon as a sphere, all the radius of spheres are R. The center of each sphere will be given in the input.

For some reasons, she wants to choose one balloon (red one or blue one) from each group, so that she can put exactly N balloons in the garden. It's obvious that there is no overlap for any two balloons in the N balloons which she selected. The largest R will make Gao's girlfriend happiest. Can you help Gao to calculate the largest R?

Input

There are multiple cases. For each case, The first line contains an integer N (2 ≤ N ≤ 200), meaning there are N groups of balloons. In the next N lines, each line contains six integers, indicating the coordinates of two balloons.

NOTICE: The garden only limits the center of the balloon.

Output

For each test case, it contains one real number indicating the largest R. The results should be rounded to three decimal places. You should promise that there is still no overlap for any two balloons after rounded.

Sample Input

2
1 1 1 5 5 5
1 1 1 5 5 5

Sample Output

3.464
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 210
#define eps 1e-6
double x[N][2],y[N][2],z[N][2];
const int maxn=505;
const int maxm=100005;
int n,m,a[maxm][3];
struct note
{
    int to;
    int next;
} edge[maxn*2*maxn];
int head[maxn];
int ip;
int dfn[maxn],low[maxn],sccno[maxn],cnt,scc,instack[maxn];
stack<int>stk;
void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}
void addedge(int u,int v)
{
    edge[ip].to=v,edge[ip].next=head[u],head[u]=ip++;
}
// x = xval or y = yval
void add_cluse(int x,int xval,int y,int yval)
{
    x=x*2+xval;
    y=y*2+yval;
    addedge(x,y^1);
    addedge(y,x^1);
}
void tarjan(int u)
{
    dfn[u]=low[u]=++scc;
    stk.push(u);
    instack[u]=1;
    for (int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if (!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if (instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if (low[u]==dfn[u])
    {
        cnt++;
        int x;
        do
        {
            x=stk.top();
            stk.pop();
            sccno[x]=cnt;
            instack[x]=0;
        }
        while (x!=u);
    }
}

inline bool reach(int i,int ival,int j,int jval,double R)
{
    return (x[i][ival]-x[j][jval])*(x[i][ival]-x[j][jval])+
           (y[i][ival]-y[j][jval])*(y[i][ival]-y[j][jval])+
           (z[i][ival]-z[j][jval])*(z[i][ival]-z[j][jval])<4*R*R;
}

bool solve(double R)
{
    init();
    for(int i=0; i<n; ++i)
        for(int a=0; a<2; ++a)
            for(int j=i+1; j<n; ++j)
                for(int b=0; b<2; ++b)
                    if(reach(i,a,j,b,R))
                        add_cluse(i,a^1,j,b^1); // a,b can't be true at the same time, so a^1 or b^1
    scc=cnt=0;
    memset(sccno,0,sizeof(sccno));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(instack,0,sizeof(instack));
    while (!stk.empty()) stk.pop();
    for (int i=0; i<2*n; i++) if (!dfn[i]) tarjan(i);
    for (int i=0; i<2*n; i+=2)
    {
        if (sccno[i]==sccno[i^1]) return false;
    }
    return true;
}

int main ()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; ++i)
        {
            for(int k=0; k<2; ++k)
                scanf("%lf%lf%lf",&x[i][k],&y[i][k],&z[i][k]);
        }
        double left=0.0,right=100000.0;
        while(right-left>eps)
        {
            double mid=(left+right)/2;
            if(solve(mid))
                left=mid;
            else right=mid;
        }
        printf("%.3lf\n",(int)(left*1000)/1000.0);
    }
    return 0;
}


你可能感兴趣的:(zoj 3717 2-sat)