zoj3717 Balloon(二分+2SAT)

来源:互联网 发布:java数据结构编程 编辑:程序博客网 时间:2024/05/17 00:08

Balloon

Time Limit: 3 Seconds      Memory Limit: 65536 KB      Special Judge

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 N groups 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

21 1 1 5 5 51 1 1 5 5 5

Sample Output

3.464

这题其实和hdu3622这题是一个题,把那个题代码随便改改交这题也能过。只不过从二维变成了三维而已。

这题要注意的只有一个点:精度!因为求出来的答案是浮点数,保留3位小数的时候会四舍五入,所以最后输出的时候要处理一下。

详情请见代码:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int N = 405;const int M = 2000005;const double eps = 1e-4;int head[N];int scc[N];int vis[N];int stack1[N];int stack2[N];int n,num;bool flag;struct node{    int x1,y1,z1,x2,y2,z2;}pt[205];double tb[205][205][4];struct edge{    int to,next;}g[M];void init(){    memset(head,-1,sizeof(head));    memset(vis,0,sizeof(vis));    memset(scc,0,sizeof(scc));    stack1[0] = stack2[0] = num = 0;    flag = true;}double dis(int x1,int y1,int z1,int x2,int y2,int z2){    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));}void maketable(){    int i,j;    for(i = 1;i < n;i ++)    {        for(j = i + 1;j <= n;j ++)        {            tb[i][j][0] = dis(pt[i].x1,pt[i].y1,pt[i].z1,pt[j].x1,pt[j].y1,pt[j].z1);            tb[i][j][1] = dis(pt[i].x1,pt[i].y1,pt[i].z1,pt[j].x2,pt[j].y2,pt[j].z2);            tb[i][j][2] = dis(pt[i].x2,pt[i].y2,pt[i].z2,pt[j].x1,pt[j].y1,pt[j].z1);            tb[i][j][3] = dis(pt[i].x2,pt[i].y2,pt[i].z2,pt[j].x2,pt[j].y2,pt[j].z2);        }    }}void build(int s,int e){    g[num].to = e;    g[num].next = head[s];    head[s] = num ++;}void dfs(int cur,int &sig,int &cnt){    if(!flag)        return;    vis[cur] = ++sig;    stack1[++stack1[0]] = cur;    stack2[++stack2[0]] = cur;    for(int i = head[cur];i != -1;i = g[i].next)    {        if(!vis[g[i].to])            dfs(g[i].to,sig,cnt);        else        {            if(!scc[g[i].to])            {                while(vis[stack2[stack2[0]]] > vis[g[i].to])                    stack2[0] --;            }        }    }    if(stack2[stack2[0]] == cur)    {        stack2[0] --;        ++ cnt;        do        {            scc[stack1[stack1[0]]] = cnt;            int tmp = stack1[stack1[0]];            if((tmp > n && scc[tmp - n] == cnt) || (tmp <= n && scc[tmp + n] == cnt))            {                flag = false;                return;            }        }while(stack1[stack1[0] --] != cur);    }}void Gabow(){    int i,sig,cnt;    sig = cnt = 0;    for(i = 1;i <= n + n && flag;i ++)        if(!vis[i])            dfs(i,sig,cnt);}bool twosat(double mid){    int i,j;    for(i = 1;i < n;i ++)    {        for(j = i + 1;j <= n;j ++)        {            if(tb[i][j][0] - mid < eps)            {                build(i,j + n);                build(j,i + n);            }            if(tb[i][j][1] - mid < eps)            {                build(i,j);                build(j + n,i + n);            }            if(tb[i][j][2] - mid < eps)            {                build(i + n,j + n);                build(j,i);            }            if(tb[i][j][3] - mid < eps)            {                build(i + n,j);                build(j + n,i);            }        }    }    Gabow();    return flag;}int main(){    int i,j;    while(~scanf("%d",&n))    {        for(i = 1;i <= n;i ++)        {            scanf("%d%d%d%d%d%d",&pt[i].x1,&pt[i].y1,&pt[i].z1,&pt[i].x2,&pt[i].y2,&pt[i].z2);        }        maketable();        double l,r,mid,ans;        l = 0.0;        r = 40000.0;        while(r - l > eps)        {            mid = (l + r)/ 2;            init();            if(twosat(mid))            {                ans = mid;                l = mid;            }            else                r = mid;        }        printf("%.3lf\n",ans/2 - 0.0005);//注意处理精度    }    return 0;}




原创粉丝点击