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. php 进度条

    <?php header( 'Content-type: text/html; charset=utf-8' ); echo 'Begin ...<br />'; for( $i = ...

  2. 【转】MFC WM_USER和WM_APP

    WM_USER常量是Windows帮助应用程序定义私有窗口类里的私有消息,通常使用WM_USER+一个整数值,但总值不能超过0x7FFF. #define WM_USER       0x0400 W ...

  3. Labview按钮的机械动作

    LabVIEW 对于按钮控件的机械动作提供了六个不同的选择,它们可以通过右键按钮并选择机械动作来找到.这些不同的选项导致按钮输出的值的行为不同.下里将这六个选项做一个简短的总结: 单击时转换当用鼠标将 ...

  4. Linux新手要了解的十个知识点

    Linux对于有的新手来说,感觉无从下手,或者不知道从哪儿学起?怎么学?针对这些问题,我给大家说说新手学习Linux需要了解的十个知识点. 注意大小写 Linux是大小写敏感的系统,举个例子,Mozi ...

  5. 原生js版ajax请求

    function getXMLHttpRequest() { var xhr; if(window.ActiveXObject) { xhr= new ActiveXObject("Micr ...

  6. 【Java面试题】40 你所知道的集合类都有哪些?主要方法?

    线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...

  7. Yii2自带验证码实现

    总共分为三个方面:控制器配置.模型rules配置和视图配置. 第一步:控制器配置 将下列代码配置在actions中,请求验证码链接对应为 “控制器/captcha” 'captcha' => [ ...

  8. error C2275: 'SOCKET' : illegal use of this type as an expression

    在VC中编译xxx.c文件出现错误error C2275 illegal use of this type as an expression 问题在于C99之前要求所有的声明必须放在函数块的起始部分, ...

  9. Windows "计划任务"功能设置闹钟~

    相信很多人和我一样在使用电脑时都会遇到这样一个麻烦:不知道如何在windows 中设置一个闹铃.当我们在“开始”菜单的所有程序中找了一遍又一遍,甚至使用Everything.exe做全盘的搜索,都没有 ...

  10. JQuery--使用autocomplete控件进行自己主动输入完毕(相当于模糊查询)

    之前为了实现这个功能花了我几天的时间. 事实上.实现了之后发现也就那么回事,正所谓万事开头难嘛.. 废话不多说了.这里我使用的是JQuery控件库中的一个Autocomplete控件.即Autocom ...