HDU 4273 Rescue(三维凸包 + 重心)

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 398    Accepted Submission(s): 296

Problem Description

I work at NASA outer space rescue team which needs much courage and patient. In daily life, I always receive a lot of mission, and I must complete it right now.

Today, team leader announced me that there is a huge spaceship dropping anchor in the out space, and we should reach there for rescue. As a working principle, at first, we should check whether there are persons living in the spaceship. So we carry a kind of
machine called life sensor which can sense the life phenomenon when the distance between the machine and the living is not farther than the sense radius.

I have read the designing paper of the spaceship in advance. It has a form of a convex polyhedron, and we can assume it is isodense. For best control, control center of the whole ship is located at the center of the mass. It is sure that if someone is still
alive, he will stay at the control center.

It‘s unfortunately that I find the door is stocked when I try to enter into the spaceship, so I can only sense the living out of the space ship. Now I have opened the machine and it‘s time to set the sense radius of it. I wonder the minimal radius of the machine
which can allowe me to check whether there are persons living in the spaceship.

Input

There are multiple test cases.

The first line contains an integer n indicating the number of vertices of the polyhedron. (4 <= n <= 100)

Each of the next n lines contains three integers xi, yi, zi, the coordinates of the polyhedron vertices (-10,000 <= xi, yi, zi <= 10,000).

It guaranteed that the given points are vertices of the convex polyhedron, and the polyhedron is non-degenerate.

Output

For each test case, output a float number indicating the minimal radius of the machine. Your answer should accurate up to 0.001.

Sample Input

4
0 0 0
1 0 0
0 1 0
0 0 1

8
0 0 0
0 0 2
0 2 0
0 2 2
2 0 0
2 0 2
2 2 0
2 2 2

Sample Output

0.144
1.000

Source

2012 ACM/ICPC Asia Regional Changchun Online

  kuangbin大神的模板一遍过。。。膜拜一下

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define eps 1e-8
#define N 110
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 answer=1e20;
        for(int i=0; i<ans.num; i++)
        {
            answer=min(answer,ans.ptoface(p,i));
        }
        printf("%.3lf\n",answer);
    }
    return 0;
}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

时间: 2024-10-10 10:37:36

HDU 4273 Rescue(三维凸包 + 重心)的相关文章

hdu4273Rescue(三维凸包重心)

链接 模板题已不叫题.. 三维凸包+凸包重心+点到平面距离(体积/点积)  体积-->混合积(先点乘再叉乘) 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<

hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***

新模板 1 /* 2 HDU 4273 Rescue 3 给一个三维凸包,求重心到表面的最短距离 4 模板题:三维凸包+多边形重心+点面距离 5 */ 6 7 #include<stdio.h> 8 #include<algorithm> 9 #include<string.h> 10 #include<math.h> 11 #include<stdlib.h> 12 using namespace std; 13 const int MAXN=

POJ 3528 hdu 3662 三维凸包模板题

POJ 3528题:http://poj.org/problem?id=3528 HDU 3662:http://acm.hdu.edu.cn/showproblem.php?pid=3662 一个是求三维凸包面数,一个是求三维凸包表面积,都是很裸的. 贴代码: #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<stdlib.h>

POJ3528 HDU3662 三维凸包模板

POJ3528 HDU3662 第一道题 给定若干点 求凸包的表面积,第二题 给定若干点就凸包的面数. 简单说一下三维凸包的求法,首先对于4个点假设不共面,确定了唯一四面体,对于一个新的点,若它不在四面体内,为了让它进入凸包, 则对于所有凸包上的边,若边的一面是该点可以看到的而另一面看不到,则该点与该边构成的面要加入凸包. 模板代码非常清晰, #include<stdio.h> #include<algorithm> #include<string.h> #includ

POJ 3528 求三维凸包表面积

也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector>

POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心

题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的各个面的最短距离,然后最短距离相加即为答案,因为显然贴着最优. 求三角形重心见此: http://www.cnblogs.com/whatbeg/p/4234518.html 代码:(模板借鉴网上模板) #include <iostream> #include <cstdio> #in

Hdu 3662 3D Convex Hull(三维凸包)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3662 思路:三维凸包模板. #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define PR 1e-8 #define N 510 using namespace std; struct TPoint { doub

HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A few naked children throw stones standing on the same position, the one throws farther win the game. Aha, of course, there are some naughty boys who care

HDU 4273

计算凸包重心到各面的最短距离. 若知道重心,按四面体用体积法即可求出高. 关键在于,多面体重心的求法.这必须把多面体分割成多个四面体来求.下面从多边形的重心说起. 一般来用,对于一个多边形(p0,p1,p2....pn-1),其重心一般为pc.x=(p0.x+p1.x+....)/n对于y也一样. 但这其实是不正确的.反例以梯形为例.上面的式子当各点的权值均匀时是正确的.(三角形是一个特例) 但在多边形上,由于面的密度一样,所以,应当是与面积有关的.于是,把多边形分割成多个三角形,求出其重心.这