HDU 4741 Save Labman No.004

来源:互联网 发布:如何把淘宝网店做好 编辑:程序博客网 时间:2024/06/05 07:13

Save Labman No.004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1179    Accepted Submission(s): 378


Problem Description
Due to the preeminent research conducted by Dr. Kyouma, human beings have a breakthrough in the understanding of time and universe. According to the research, the universe in common sense is not the only one. Multi World Line is running simultaneously. In simplicity, let us use a straight line in three-dimensional coordinate system to indicate a single World Line.

During the research in World Line Alpha, the assistant of Dr. Kyouma, also the Labman No.004, Christina dies. Dr. Kyouma wants to save his assistant. Thus, he has to build a Time Tunnel to jump from World Line Alpha to World Line Beta in which Christina can be saved. More specifically, a Time Tunnel is a line connecting World Line Alpha and World Line Beta. In order to minimizing the risks, Dr. Kyouma wants you, Labman No.003 to build a Time Tunnel with shortest length.
 

Input
The first line contains an integer T, indicating the number of test cases.

Each case contains only one line with 12 float numbers (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4), correspondingly indicating two points in World Line Alpha and World Line Beta. Note that a World Line is a three-dimensional line with infinite length.

Data satisfy T <= 10000, |x, y, z| <= 10,000.
 

Output
For each test case, please print two lines.

The first line contains one float number, indicating the length of best Time Tunnel.

The second line contains 6 float numbers (xa, ya, za), (xb, yb, zb), seperated by blank, correspondingly indicating the endpoints of the best Time Tunnel in World Line Alpha and World Line Beta.

All the output float number should be round to 6 digits after decimal point. Test cases guarantee the uniqueness of the best Time Tunnel.
 

Sample Input
11 0 1 0 1 1 0 0 0 1 1 1
 

Sample Output
0.4082480.500000 0.500000 1.000000 0.666667 0.666667 0.666667
 

Source
2013 ACM/ICPC Asia Regional Hangzhou Online
 

Recommend
liuyiding
 
  刚开始理解错题意了,以为是线段之间的最短距离呢,这时候要用到(直线到直线的最短距离,点到线段的最短距离),最后错了很多次后发现题意是直线之间,那么这题的复杂程度就降低了。
直线到直线的最短距离 思路:
直线1:
p1 = x1 + t*(x2-x1);
q1 = y1 + t*(y2-y1);
r1 = z1 + t*(z2-z1);
直线2:
p2 = x3 + s*(x4-x3);
q2 = y3 + s*(y4-y3);
r2 = z3 + s*(z4-z3);
 
距离的平方也就是dis*dis= (p1-p2)*(p1-p2)+(q2-q1)*(q2-q1)+(r2-r1)*(r2-r1);
化简后 就只有s和t组成的函数,然后分别对s和t 求偏导数,求出s和 t,本题得到解决
注意这种思路要用long double,输入输出用double,卡精度啊
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#define eqs 1e-10using namespace std;int main(){    //freopen("data.in","r",stdin);    int t;    scanf("%d",&t);    while(t--)    {        double x1_1,y1_1,z1_1;        double x2_1,y2_1,z2_1;        double x3_1,y3_1,z3_1;        double x4_1,y4_1,z4_1;        scanf("%lf %lf %lf",&x1_1,&y1_1,&z1_1);        scanf("%lf %lf %lf",&x2_1,&y2_1,&z2_1);        scanf("%lf %lf %lf",&x3_1,&y3_1,&z3_1);        scanf("%lf %lf %lf",&x4_1,&y4_1,&z4_1);        long double x1,y1,z1;        long double x2,y2,z2;        long double x3,y3,z3;        long double x4,y4,z4;        x1 = x1_1; y1 = y1_1; z1 = z1_1;        x2 = x2_1; y2 = y2_1; z2 = z2_1;        x3 = x3_1; y3 = y3_1; z3 = z3_1;        x4 = x4_1; y4 = y4_1; z4 = z4_1;        long double a1 = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1);        long double b1 = (x3-x1)*(x2-x1)+(y3-y1)*(y2-y1)+(z3-z1)*(z2-z1);        long double c1 = (x4-x3)*(x2-x1)+(y4-y3)*(y2-y1)+(z4-z3)*(z2-z1);        long double a2 = c1;        long double b2 = (x3-x1)*(x4-x3)+(y3-y1)*(y4-y3)+(z3-z1)*(z4-z3);        long double c2 = (x4-x3)*(x4-x3)+(y4-y3)*(y4-y3)+(z4-z3)*(z4-z3);        long double k = (b1*c2-b2*c1)/(a1*c2-a2*c1);        long double s ;        if(fabs(c1-0.0)<=eqs)        {            s =(k*a2-b2)/c2;        }else        {            s=(k*a1-b1)/c1;        }        long double resx1 = x1+k*(x2-x1);        long double resy1 = y1+k*(y2-y1);        long double resz1 = z1+k*(z2-z1);        long double resx2 = x3+s*(x4-x3);        long double resy2 = y3+s*(y4-y3);        long double resz2 = z3+s*(z4-z3);        long double dis = (resx1-resx2)*(resx1-resx2)+(resy1-resy2)*(resy1-resy2)+(resz1-resz2)*(resz1-resz2);        printf("%.6lf\n",sqrt((double)dis));        printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",(double)resx1,(double)resy1,(double)resz1,(double)resx2,(double)resy2,(double)resz2);    }    return 0;}

原创粉丝点击