2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)

Save Labman No.004

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


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
   
   
   
   
1 1 0 1 0 1 1 0 0 0 1 1 1
 

Sample Output
   
   
   
   
0.408248 0.500000 0.500000 1.000000 0.666667 0.666667 0.666667
 

Source
2013 ACM/ICPC Asia Regional Hangzhou Online
 

Recommend
liuyiding
 
题目大意:当时看见这个题目蛮激动的,因为求异面直线的最短距离前不久还写过博客,不过那样只能求最短距离。当时被迫与无奈。题目大意很简单,给你两条直线,问你最短距离是多少,并且求出最短距离的点,需要求得点是唯一的。

解题思路:当时自己想的就是先把两条直线求出来,然后分别在两条直线上找俩点p和q。这样未知数就有两个,然后再求公垂线,根据公垂线与pq共线xyz三维坐标即可列出三元一次方程组,未知数三个,方程有三个。只是算起来不是很顺手。详见AC代码。

题目地址:Save Labman No.004

AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    int tes;
    double x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4;
    double a1,b1,c1,a2,b2,c2;
    scanf("%d",&tes);
    while(tes--)
    {
            scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&z1,&x2,&y2,&z2,&x3,&y3,&z3,&x4,&y4,&z4);
            a1=x2-x1,b1=y2-y1,c1=z2-z1;  //第一条直线
            a2=x4-x3,b2=y4-y3,c2=z4-z3;  //第二条直线
            double k1,k2;
            double p1,p2,p3;  //p点
            double q1,q2,q3;  //q点
            double s1,s2,s3;  //前两条直线的公垂线
            s1=b1*c2-b2*c1;
            s2=c1*a2-c2*a1;
            s3=a1*b2-a2*b1;
            //方程组转化为
            //t1*k1+t2*k2=t3,m1*k1+m2*k2=m3;
            double t1,t2,t3,m1,m2,m3;
            t1=s2*a1-s1*b1,t2=s1*b2-s2*a2,t3=s1*(y1-y3)-s2*(x1-x3);
            m1=s3*a1-s1*c1,m2=s1*c2-s3*a2,m3=s1*(z1-z3)-s3*(x1-x3);
            k1=(t3*m2-m3*t2)/(t1*m2-m1*t2);
            k2=(t3*m1-m3*t1)/(t2*m1-m2*t1);

            p1=k1*a1+x1,p2=k1*b1+y1,p3=k1*c1+z1,
            q1=k2*a2+x3,q2=k2*b2+y3,q3=k2*c2+z3;
            double dis=sqrt((p1-q1)*(p1-q1)+(p2-q2)*(p2-q2)+(p3-q3)*(p3-q3));
            printf("%.6lf\n",dis);
            printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",p1,p2,p3,q1,q2,q3);
    }
    return 0;
}

/*
23
0 0 0 0 0 1 1 0 0 0 1 0
*/
//234MS 248K


你可能感兴趣的:(2013网络赛)