POJ 3862 Asteroids(两个三维凸包的重心到表面最短距离和)

Asteroids
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 536 Accepted: 184 Special Judge

Description

Association of Collision Management (ACM) is planning to perform the controlled collision of two asteroids. The asteroids will be slowly brought together and collided at negligible speed. ACM expects asteroids to get attached to each other and form a stable object. 
Each asteroid has the form of a convex polyhedron. To increase the chances of success of the experiment ACM wants to bring asteroids together in such manner that their centers of mass are as close as possible. To achieve this, ACM operators can rotate the asteroids and move them independently before bringing them together. 
Help ACM to find out what minimal distance between centers of mass can be achieved. 
For the purpose of calculating center of mass both asteroids are considered to have constant density.

Input

Input file contains two descriptions of convex polyhedra. 
The first line of each description contains integer number n - the number of vertices of the polyhedron (4 <= n <= 60). The following n lines contain three integer numbers xi, yi, zi each - the coordinates of the polyhedron vertices (-10 4<= xi, yi, zi <= 10 4). It is guaranteed that the given points are vertices of a convex polyhedron, in particular no point belongs to the convex hull of other points. Each polyhedron is non-degenerate. 
The two given polyhedra have no common points.

Output

Output one floating point number - the minimal distance between centers of mass of the asteroids that can be achieved. Your answer must be accurate up to 10 -5.

Sample Input

8
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
5
0 0 5
1 0 6
-1 0 6
0 1 6
0 -1 6

Sample Output

0.75
和上一题一样 只不过多求一个凸包
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define N 61
#define ll long long int
#define eps 1e-8
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
struct point
{
    double x,y,z;
    point() {}
    point(double xx,double yy,double zz): x(xx),y(yy),z(zz) {}
    point operator -(const point p)
    {
        return point(x-p.x, y-p.y, z-p.z);
    }
    point operator +(const point p)
    {
        return point(x+p.x, y+p.y, z+p.z);
    }
    point operator *(const point p)
    {
        return point(y*p.z-z*p.y, z*p.x-x*p.z, x*p.y-y*p.x);
    }
    point operator *(double p)
    {
        return point(x*p, y*p,z*p);
    }
    point operator /(double p)
    {
        return point(x/p, y/p,z/p);
    }
    double operator ^( point p)
    {
        return x*p.x+y*p.y+z*p.z;
    }

};
struct node
{
    struct face
    {
        int a,b,c;
        bool ok;
    };
    int n;
    point tn[N];
    int num;
    face F[8*N];
    int g[N][N];
    double vlen(point a)
    {
        return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
    }
    point cross(const point &a,const point &b,const point &c)
    {
        return point ( (b.y-a.y)*(c.z-a.z) - (b.z-a.z)*(c.y-a.y),
                       (b.z-a.z)*(c.x-a.x) - (b.x-a.x)*(c.z-a.z),
                       (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x)

                     );
    }
    double area(point a,point b,point c)
    {
        return vlen((b*a)*(c-a));
    }
    double volume(point a,point b,point c,point d)
    {
        return (b-a)*(c-a)^(d-a);
    }
    double dbcmp(point &p,face &f)
    {
        point m = tn[f.b] - tn[f.a];
        point n = tn[f.c] - tn[f.a];
        point t = p - tn[f.a];
        return (m*n)^t;
    }
    void deal(int p,int a,int b)
    {
        int f = g[a][b];
        face add;
        if(F[f].ok)
        {
            if(dbcmp(tn[p],F[f]) > eps)
                dfs(p,f);
            else
            {
                add.a=b;
                add.b=a;
                add.c=p;
                add.ok=true;
                g[p][b] = g[a][p] = g[b][a] =num;
                F[num++] = add;
            }
        }
    }
    void dfs(int p,int now)
    {
        F[now].ok = false;
        deal(p,F[now].b,F[now].a);
        deal(p,F[now].c,F[now].b);
        deal(p,F[now].a,F[now].c);

    }
    bool same(int s,int t)
    {
        point &a = tn[F[s].a];
        point &b = tn[F[s].b];
        point &c = tn[F[s].c];
        return fabs( volume(a,b,c,tn[F[t].a])) < eps &&
               fabs( volume(a,b,c,tn[F[t].b])) < eps &&
               fabs( volume(a,b,c,tn[F[t].c])) < eps ;
    }
    void create()
    {
        int i,j,tmp;
        face add;
        num = 0;
        if(n<4) return ;
        bool flag =true;
        for(i=1; i<n; i++)
        {
            if(vlen(tn[0]-tn[i]) > eps)
            {
                swap(tn[1],tn[i]);
                flag = false;
                break;
            }
        }
        if(flag) return ;
        flag =true;
        for(i=2; i<n; i++)
        {
            if(vlen((tn[0]-tn[1])*(tn[1]-tn[i])) > eps )
            {
                swap(tn[2],tn[i]);
                flag =false;
                break;
            }
        }
        if(flag) return ;
        flag =true;
        for(i =3; i<n; i++)
        {
            if(fabs((tn[0]-tn[1])*(tn[1]-tn[2])^(tn[0]-tn[i])) >eps    )
            {
                swap(tn[3],tn[i]);
                flag =false;
                break;
            }
        }
        if(flag) return ;
        for(i=0; i<4; i++)
        {
            add.a = (i+1)%4;
            add.b = (i+2)%4;
            add.c = (i+3)%4;
            add.ok = true;
            if(dbcmp(tn[i],add) > 0)
            {
                swap(add.b,add.c);
            }
            g[add.a][add.b] =   g[add.b][add.c] =  g[add.c][add.a] =num;
            F[num++]=add;
        }
        for(i =4; i<n; i++)
        {
            for(j =0; j<num; j++)
            {
                if(F[j].ok && dbcmp(tn[i],F[j])>eps)
                {
                    dfs(i,j);
                    break;
                }
            }
        }
        tmp =num;
        for(i=num=0; i<tmp; i++)
        {
            if(F[i].ok)
                F[num++] = F[i];
        }
    }
    double area()
    {
        double res=0;
        if(n==3)
        {
            point p = cross(tn[0],tn[1],tn[2]);
            res = vlen(p)/2.0;
            return res;
        }
        for(int i=0; i<num; i++)
        {
            res += area(tn[F[i].a],tn[F[i].b],tn[F[i].c]);
        }
        return res/2.0;
    }
    double volume()
    {
        double res=0;
        point tmp(0,0,0);
        for(int i = 0; i<num; i++)
            res += volume(tmp,tn[F[i].a],tn[F[i].b],tn[F[i].c]);
        return fabs(res/6.0);
    }
    int triangle()
    {
        return num;
    }
    int polygon()
    {
        int i,j,res,flag;
        for(i=res=0; i<num; i++)
        {
            flag=1;
            for(j=0; j<i; j++)
            {
                if(same(i,j))
                {
                    flag=0;
                    break;
                }
            }
            res+=flag;
        }
        return res;
    }
    point barycenter()
    {
        point ans(0,0,0),o(0,0,0);
        double all=0;
        for(int i=0; i<num; i++)
        {
            double vol = volume(o,tn[F[i].a],tn[F[i].b],tn[F[i].c]);
            ans = ans+(o+tn[F[i].a]+tn[F[i].b]+tn[F[i].c])/4.0*vol;
            all+=vol;
        }
        ans = ans/all;
        return ans;
    }
    double ptoface(point p,int i)
    {
        return fabs(volume(tn[F[i].a],tn[F[i].b],tn[F[i].c],p) /
                    vlen( (tn[F[i].b]-tn[F[i].a])*(tn[F[i].c]-tn[F[i].a]) ));
    }
};
node ans;
int main()
{
    while(~scanf("%d",&ans.n))
    {
        for(int i=0; i<ans.n; i++)
        {
            scanf("%lf%lf%lf",&ans.tn[i].x,&ans.tn[i].y,&ans.tn[i].z);
        }
        ans.create();
        point p=ans.barycenter();
        double answer1=1e20;
        for(int i=0; i<ans.num; i++)
        {
            answer1=min(answer1,ans.ptoface(p,i));
        }

        scanf("%d", &ans.n);
         for(int i=0; i<ans.n; i++)
        {
            scanf("%lf%lf%lf",&ans.tn[i].x,&ans.tn[i].y,&ans.tn[i].z);
        }
        ans.create();
         p = ans.barycenter();
        double answer2=1e20;
        for(int i=0; i<ans.num; i++)
        {
            answer2=min(answer2,ans.ptoface(p,i));
        }
        printf("%.5lf\n",answer1 + answer2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值