题意:给出空间两条线段,求距离。

注意输出格式!

 #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; struct Point3
{
int x, y, z;
Point3(int x=, int y=, int z=):x(x),y(y),z(z) { }
}; typedef Point3 Vector3; Vector3 operator + (const Vector3& A, const Vector3& B)
{
return Vector3(A.x+B.x, A.y+B.y, A.z+B.z);
}
Vector3 operator - (const Point3& A, const Point3& B)
{
return Vector3(A.x-B.x, A.y-B.y, A.z-B.z);
}
Vector3 operator * (const Vector3& A, int p)
{
return Vector3(A.x*p, A.y*p, A.z*p);
} bool operator == (const Point3& a, const Point3& b)
{
return a.x==b.x && a.y==b.y && a.z==b.z;
} Point3 read_point3()
{
Point3 p;
scanf("%d%d%d", &p.x, &p.y, &p.z);
return p;
} int Dot(const Vector3& A, const Vector3& B)
{
return A.x*B.x + A.y*B.y + A.z*B.z;
}
int Length2(const Vector3& A)
{
return Dot(A, A);
}
Vector3 Cross(const Vector3& A, const Vector3& B)
{
return Vector3(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x);
} typedef long long LL; LL gcd(LL a, LL b)
{
return b ? gcd(b, a%b) : a;
}
LL lcm(LL a, LL b)
{
return a / gcd(a,b) * b;
} struct Rat
{
LL a, b;
Rat(LL a=):a(a),b() { }
Rat(LL x, LL y):a(x),b(y)
{
if(b < ) a = -a, b = -b;
LL d = gcd(a, b);
if(d < ) d = -d;
a /= d;
b /= d;
}
}; Rat operator + (const Rat& A, const Rat& B)
{
LL x = lcm(A.b, B.b);
return Rat(A.a*(x/A.b)+B.a*(x/B.b), x);
} Rat operator - (const Rat& A, const Rat& B)
{
return A + Rat(-B.a, B.b);
}
Rat operator * (const Rat& A, const Rat& B)
{
return Rat(A.a*B.a, A.b*B.b);
} void updatemin(Rat& A, const Rat& B)
{
if(A.a*B.b > B.a*A.b) A.a = B.a, A.b = B.b;
} // 点P到线段AB的距离的平方
Rat Rat_Distance2ToSegment(const Point3& P, const Point3& A, const Point3& B)
{
if(A == B) return Length2(P-A);
Vector3 v1 = B - A, v2 = P - A, v3 = P - B;
if(Dot(v1, v2) < ) return Length2(v2);
else if(Dot(v1, v3) > ) return Length2(v3);
else return Rat(Length2(Cross(v1, v2)), Length2(v1));
} // 求异面直线p1+su和p2+tv的公垂线对应的s。如果平行/重合,返回false
bool Rat_LineDistance3D(const Point3& p1, const Vector3& u, const Point3& p2, const Vector3& v, Rat& s)
{
LL b = (LL)Dot(u,u)*Dot(v,v) - (LL)Dot(u,v)*Dot(u,v);
if(b == ) return false;
LL a = (LL)Dot(u,v)*Dot(v,p1-p2) - (LL)Dot(v,v)*Dot(u,p1-p2);
s = Rat(a, b);
return true;
} void Rat_GetPointOnLine(const Point3& A, const Point3& B, const Rat& t, Rat& x, Rat& y, Rat& z)
{
x = Rat(A.x) + Rat(B.x-A.x) * t;
y = Rat(A.y) + Rat(B.y-A.y) * t;
z = Rat(A.z) + Rat(B.z-A.z) * t;
} Rat Rat_Distance2(const Rat& x1, const Rat& y1, const Rat& z1, const Rat& x2, const Rat& y2, const Rat& z2)
{
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
} int main()
{
int T;
scanf("%d", &T);
LL maxx = ;
while(T--)
{
Point3 A = read_point3();
Point3 B = read_point3();
Point3 C = read_point3();
Point3 D = read_point3();
Rat s, t;
bool ok = false;
Rat ans = Rat();
if(Rat_LineDistance3D(A, B-A, C, D-C, s))
if(s.a > && s.a < s.b && Rat_LineDistance3D(C, D-C, A, B-A, t))
if(t.a > && t.a < t.b)
{
ok = true; // 异面直线/相交直线
Rat x1, y1, z1, x2, y2, z2;
Rat_GetPointOnLine(A, B, s, x1, y1, z1);
Rat_GetPointOnLine(C, D, t, x2, y2, z2);
ans = Rat_Distance2(x1, y1, z1, x2, y2, z2);
}
if(!ok) // 平行直线/重合直线
{
updatemin(ans, Rat_Distance2ToSegment(A, C, D));
updatemin(ans, Rat_Distance2ToSegment(B, C, D));
updatemin(ans, Rat_Distance2ToSegment(C, A, B));
updatemin(ans, Rat_Distance2ToSegment(D, A, B));
}
printf("%lld %lld\n", ans.a, ans.b);
}
return ;
}

uvalive 4973 Ardenia的更多相关文章

  1. LA 4973 Ardenia (3D Geometry + Simulation)

    ACM-ICPC Live Archive 三维几何,题意是要求求出两条空间线段的距离.题目难度在于要求用有理数的形式输出,这就要求写一个有理数类了. 开始的时候写出来的有理数类就各种疯狂乱套,TLE ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

随机推荐

  1. httpsclient 自动获取证书 无证书访问 验证过能直接用

    首先实现写一个 实现接口SecureProtocolSocketFactory的类. /** *ClassName: bcde *date: 2015年2月26日 下午4:51:01 * *@auth ...

  2. python调试总结

    调试通常采用两种方式,打印日志调试以及运行时实时跟踪调试. 一.打印日志: 1. print不要看不起print,这是一切调试的起点,即便是调试Java或者C这种巨麻烦的编译语言,print仍然是常用 ...

  3. mac 用 brew

    mac 下用   brew 安装插件, 有重复的不会再次安装,比xmap模式好

  4. windows8 64位 IIS8 PHP5.5 安装 Imagemagick 组件

    为什么这里一定要说 windows 系统是64位呢,因为如果是系统是64位,那么PHP5.5 一般都会选择64的, Imagemagick 组件也会选择64位的, 但是操蛋的是 64位的Imagema ...

  5. javaweb学习总结(四十五)——监听器(Listener)学习二

    一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...

  6. 封装SqlHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  7. SQL盲注修订建议

    一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...

  8. 安装Ubuntu双系统系列——安装中文输入法

    Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架.在Ubuntu的中文 ...

  9. 两页pdf打印为一页,并且放大(打印英文pdf常用)

    多很英文书籍都是小书,若我们直接打印它的pdf会很厚,比如我要打印一本 thinking in C++,就要800+页.不如把两页打成一页.但是打成一页之后又太小了,需要放大.具体方法如下:   前提 ...

  10. 无效的 URI: 未能分析证书颁发机构/主机

    出 现该错误的原因是URL中少了一个斜杠,正常的URL是“http:”后边有两个斜杠,而我在修改配置文件中的URL的IP地址部分时,不小心删掉了一个 斜杠,例如:http:/blog.csdn.net ...