Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 481   Accepted: 152   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 (-104 <= xi,
yi, zi <= 104). 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"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stack"
#include"vector"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 533
#define eps 1e-10
#define inf 0x3f3f3f3f
#define mod 1070000009
#define PI acos(-1.0)
using namespace std;
struct node
{
double x,y,z,dis;
node(){}
node(double xx,double yy,double zz):x(xx),y(yy),z(zz){}
node operator +(const node p)
{
return node(x+p.x,y+p.y,z+p.z);
}
node operator -(const node p)
{
return node(x-p.x,y-p.y,z-p.z);
}
node operator *(const node p)
{
return node(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
}
node operator *(const double p)
{
return node(x*p,y*p,z*p);
}
node operator /(const double p)
{
return node(x/p,y/p,z/p);
}
double operator ^(const node p)
{
return x*p.x+y*p.y+z*p.z;
}
};
struct threeD_convex_hull
{
struct face
{
int a,b,c;
int ok;
};
int n;
int cnt;
node p[M];
face f[M*8];
int to[M][M];
double len(node p)
{
return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
}
double area(node a,node b,node c)
{
return len((b-a)*(c-a));
}
double volume(node a,node b,node c,node d)
{
return (b-a)*(c-a)^(d-a);
}
double ptof(node q,face f)
{
node m=p[f.b]-p[f.a];
node n=p[f.c]-p[f.a];
node t=q-p[f.a];
return m*n^t;
}
void dfs(int q,int cur)
{
f[cur].ok=0;
deal(q,f[cur].b,f[cur].a);
deal(q,f[cur].c,f[cur].b);
deal(q,f[cur].a,f[cur].c);
}
void deal(int q,int a,int b)
{
int fa=to[a][b];
face add;
if(f[fa].ok)
{
if(ptof(p[q],f[fa])>eps)
dfs(q,fa);
else
{
add.a=b;
add.b=a;
add.c=q;
add.ok=1;
to[b][a]=to[a][q]=to[q][b]=cnt;
f[cnt++]=add;
}
}
}
int same(int s,int t)
{
node a=p[f[s].a];
node b=p[f[s].b];
node c=p[f[s].c];
if(fabs(volume(a,b,c,p[f[t].a]))<eps
&&fabs(volume(a,b,c,p[f[t].b]))<eps
&&fabs(volume(a,b,c,p[f[t].c]))<eps)
return 1;
return 0;
}
void make()
{
cnt=0;
if(n<4)
return;
int sb=1;
for(int i=1;i<n;i++)
{
if(len(p[0]-p[i])>eps)
{
swap(p[1],p[i]);
sb=0;
break;
}
}
if(sb)return;
sb=1;
for(int i=2;i<n;i++)
{
if(len((p[1]-p[0])*(p[i]-p[0]))>eps)
{
swap(p[2],p[i]);
sb=0;
break;
}
}
if(sb)return;
sb=1;
for(int i=3;i<n;i++)
{
if(fabs(volume(p[0],p[1],p[2],p[i]))>eps)
{
swap(p[3],p[i]);
sb=0;
break;
}
}
if(sb)return;
face add;
for(int i=0;i<4;i++)
{
add.a=(i+1)%4;
add.b=(i+2)%4;
add.c=(i+3)%4;
add.ok=1;
if(ptof(p[i],add)>eps)
swap(add.c,add.b);
to[add.a][add.b]=to[add.b][add.c]=to[add.c][add.a]=cnt;
f[cnt++]=add;
}
for(int i=4;i<n;i++)
{
for(int j=0;j<cnt;j++)
{
if(f[j].ok&&ptof(p[i],f[j])>eps)
{
dfs(i,j);
break;
}
}
}
int tmp=cnt;
cnt=0;
for(int i=0;i<tmp;i++)
if(f[i].ok)
f[cnt++]=f[i];
}
double Area()//表面积
{
double S=0;
if(n==3)
{
S=area(p[0],p[1],p[2])/2.0;
return S;
}
for(int i=0;i<cnt;i++)
S+=area(p[f[i].a],p[f[i].b],p[f[i].c]);
return S/2.0;
}
double Volume()//体积
{
double V=0;
node mid(0,0,0);
for(int i=0;i<cnt;i++)
V+=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
V=fabs(V)/6.0;
return V;
}
int tringleCnt()
{
return cnt;
}
int faceCnt()
{
int num=0;
for(int i=0;i<cnt;i++)
{
int flag=1;
for(int j=0;j<i;j++)
{
if(same(i,j))
{
flag=0;
break;
}
}
num+=flag;
}
return num;
}
double pf_dis(face f,node q)//点到面的距离
{
double V=volume(p[f.a],p[f.b],p[f.c],q);
double S=area(p[f.a],p[f.b],p[f.c]);
return fabs(V/S);
}
double min_dis(node q)//暴力搜索内部的点q到面的最短距离即体积/面积
{
double mini=inf;
for(int i=0;i<cnt;i++)
{
double h=pf_dis(f[i],q);
if(mini>h)
mini=h;
}
return mini;
}
node barycenter()
{
node ret(0,0,0),mid(0,0,0);
double sum=0;
for(int i=0;i<cnt;i++)
{
double V=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
ret=ret+(mid+p[f[i].a]+p[f[i].b]+p[f[i].c])/4.0*V;
sum+=V;
}
ret=ret/sum;
return ret;
} }hull;
/*int main()
{
while(scanf("%d",&hull.n)!=EOF)
{
for(int i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.p[i].x,&hull.p[i].y,&hull.p[i].z);
hull.make();
printf("%d\n",hull.faceCnt());
}
return 0;
}*/
int main()
{
while(scanf("%d",&hull.n)!=-1)
{
for(int i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.p[i].x,&hull.p[i].y,&hull.p[i].z);
hull.make();
node center=hull.barycenter();
double min1=hull.min_dis(center);
scanf("%d",&hull.n);
for(int i=0;i<hull.n;i++)
scanf("%lf%lf%lf",&hull.p[i].x,&hull.p[i].y,&hull.p[i].z);
hull.make();
center=hull.barycenter();
double min2=hull.min_dis(center);
printf("%.5lf\n",min1+min2);
}
return 0;
}

三维凸包(两个没有公共点)经过旋转平移后使其重心相距最近(POJ3862)的更多相关文章

  1. hdu4449Building Design(三维凸包+平面旋转)

    链接 看了几小时也没看懂代码表示的何意..无奈下来问问考研舍友. 还是考研舍友比较靠谱,分分钟解决了我的疑问. 可能三维的东西在纸面上真的不好表示,网上没有形象的题解,只有简单"明了&quo ...

  2. hdu4273Rescue(三维凸包重心)

    链接 模板题已不叫题.. 三维凸包+凸包重心+点到平面距离(体积/点积)  体积-->混合积(先点乘再叉乘) #include <iostream> #include<cstd ...

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

    题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...

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

    新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...

  5. bzoj 1964: hull 三维凸包 计算几何

    1964: hull 三维凸包 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 54  Solved: 39[Submit][Status][Discuss ...

  6. POJ 3528 求三维凸包表面积

    也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...

  7. 三维凸包求其表面积(POJ3528)

    Ultimate Weapon Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 2074   Accepted: 989 D ...

  8. 三维凸包求凸包表面的个数(HDU3662)

    3D Convex Hull Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. 三维凸包求重心到面的最短距离(HDU4273)

    http://acm.hdu.edu.cn/showproblem.php?pid=4273 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memo ...

随机推荐

  1. HBase系统入门--整体介绍

    转自:http://www.aboutyun.com/thread-8957-1-2.html 问题导读:1.HBase查询与写入哪个更好一些?2.HBase面对复杂操作能否实现?3.Region服务 ...

  2. imx6 spi分析

    /************************************************************************** *本文主要跟踪imx6 spi设备和驱动的注册过 ...

  3. 正则表达式”\d+\.?\d*”在匹配下列字符串时结果是失败的是?

    A 12.5 B 1.25 C 以上都成功 D 以上都失败 解答:B \d+ 表示可以出现1次或是n次数字 \. .? 表示可以“.”可以出现一次,也可以不出现 \d* 表示可以出现0次或是n次数字

  4. C# 过滤sql特殊字符方法集合

    1./// <summary>    /// 过滤不安全的字符串    /// </summary>    /// <param name="Str" ...

  5. 关于在SQLITE数据库表中插入本地系统时间的做法

    首先,我参考下面的博文地址:http://blog.csdn.net/liuzhidong123/article/details/6847104 sqlite3 表里插入系统时间(时间戳) 分类: s ...

  6. ubuntu 访问 共享 windows文件夹

    sudo mount -o username=*******,password=******** //192.168.1.105/迅雷下载 /mnt/

  7. 一个窗口里包含一个iframe,点击iframe内的submit按钮,返回的视图总是显示在iframe中,我想要的效果是点击按钮后返回的视图是在浏览器窗口中...?asp.net mvc 的action中,不用js怎么实现??????????

    Content("<script type='text/javascript'>parent.location.href = '" + url + "';&l ...

  8. VCL 中的 Windows API 函数(6): BeginDeferWindowPos

    BeginDeferWindowPos 和 DeferWindowPos.EndDeferWindowPos 是一组一起使用的函数, 可对一组窗口的位置.大小.Z 序等进行调整, 在 ExtCtrls ...

  9. cesium图形上加载图片

    <!DOCTYPE html> <html> <head> <!-- Use correct character set. --> <meta c ...

  10. linux下mysql 启动命令

    1,使用service 启动.关闭MySQL服务 service mysql start service mysql stop service mysql restart 运行上面命令,其实是serv ...