hdu 4617 Weapon

时间:2021-12-17 12:51:32

http://acm.hdu.edu.cn/showproblem.php?pid=4617

三维几何简单题 多谢高尚博学长留下的模板

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const int MAXL = (1 << 18)+100;
const double eps = (1e-9);
const int N=50;
struct point3{double x,y,z;};
struct line3{point3 a,b;};
double r[N];
line3 L[N];
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
point3 xmult(point3 u,point3 v)
{
point3 ret;
ret.x=u.y*v.z-v.y*u.z;
ret.y=u.z*v.x-u.x*v.z;
ret.z=u.x*v.y-u.y*v.x;
return ret;
}
double dmult(point3 u,point3 v)
{
return u.x*v.x+u.y*v.y+u.z*v.z;
}
point3 subt(point3 u,point3 v)
{
point3 ret;
ret.x=u.x-v.x;
ret.y=u.y-v.y;
ret.z=u.z-v.z;
return ret;
}
double vlen(point3 p)
{
return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
}
double dist(point3 u,point3 v)
{
return (double)(sqrt((u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y)+(u.z-v.z)*(u.z-v.z)));
}
double ptoline(point3 p,line3 l)
{
l.b.x=l.a.x+l.b.x;
l.b.y=l.a.y+l.b.y;
l.b.z=l.a.z+l.b.z;
return vlen(xmult(subt(p,l.a),subt(l.b,l.a)))/dist(l.a,l.b);
}
double linetoline(line3 u,line3 v)
{
point3 n=xmult(u.b,v.b);
if(dcmp(vlen(n))==0)
return ptoline(u.a,v);
return fabs(dmult(subt(v.a,u.a),n))/vlen(n);
}
point3 pvec(point3 s1,point3 s2,point3 s3)
{
return xmult(subt(s1,s2),subt(s2,s3));
} int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
point3 p[3];
for(int j=0;j<3;++j)
scanf("%lf %lf %lf",&p[j].x,&p[j].y,&p[j].z);
L[i].a=p[0];
L[i].b=pvec(p[0],p[1],p[2]);
r[i]=dist(p[0],p[1]);
}
double k=1e9;
for(int i=0;i<n;++i)
for(int j=i+1;j<n;++j)
{
//cout<<linetoline(L[i],L[j])<<endl;
k=min(k,linetoline(L[i],L[j])-r[i]-r[j]);
}
if(dcmp(k)<=0)
printf("Lucky\n");
else
printf("%.2lf\n",k); }
return 0;
}