HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)

Save Labman No.004

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


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
 

 

Recommend
liuyiding
 

 

详细公式见:

http://blog.sina.com.cn/s/blog_a401a1ea0101ij9z.html

 

 

然后直接使用公式就可以了。

 

 

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013/9/15 星期日 13:01:15
  4 File Name     :2013杭州网络赛\1004.cpp
  5 ************************************************ */
  6 
  7 #pragma comment(linker, "/STACK:1024000000,1024000000")
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <iostream>
 11 #include <algorithm>
 12 #include <vector>
 13 #include <queue>
 14 #include <set>
 15 #include <map>
 16 #include <string>
 17 #include <math.h>
 18 #include <stdlib.h>
 19 #include <time.h>
 20 using namespace std;
 21 struct Point
 22 {
 23     double x,y,z;
 24     Point(double _x = 0,double _y = 0,double _z = 0)
 25     {
 26         x = _x;
 27         y = _y;
 28         z = _z;
 29     }
 30     Point operator +(const Point &b)const
 31     {
 32         return Point(x + b.x, y + b.y,z + b.z);
 33     }
 34     Point operator -(const Point &b)const
 35     {
 36         return Point(x - b.x, y - b.y,z - b.z);
 37     }
 38     Point operator /(double k)
 39     {
 40         return Point(x/k,y/k,z/k);
 41     }
 42     Point operator *(double k)
 43     {
 44         return Point(x*k,y*k,z*k);
 45     }
 46     double operator *(const Point &b)const
 47     {
 48         return x*b.x + y*b.y + z*b.z;
 49     }
 50     Point operator ^(const Point &b)const
 51     {
 52         return Point(y*b.z - z *b.y,z*b.x - x*b.z, x*b.y - y * b.x);
 53     }
 54     void input()
 55     {
 56         scanf("%lf%lf%lf",&x,&y,&z);
 57     }
 58     void output()
 59     {
 60         printf("%.6lf %.6lf %.6lf",x,y,z);
 61     }
 62 };
 63 double dis(Point a,Point b)
 64 {
 65     return sqrt((a.x - b.x) * (a.x- b.x)  + (a.y - b.y) *(a.y - b.y) + (a.z - b.z)*(a.z - b.z));
 66 }
 67 double norm(Point a)
 68 {
 69     return sqrt(a.x *a.x + a.y *a.y + a.z * a.z);
 70 }
 71 Point A,B,C,D;
 72 Point mid1,mid2;
 73 int main()
 74 {
 75     //freopen("in.txt","r",stdin);
 76     //freopen("out.txt","w",stdout);
 77     int T;
 78     scanf("%d",&T);
 79     while(T--)
 80     {
 81         A.input();
 82         B.input();
 83         C.input();
 84         D.input();
 85         Point p1 = (B-A);
 86         Point p2 = (D-C);
 87         Point p = p1^p2;
 88 
 89         double dd = (p*(A-C))/norm(p);
 90         dd = fabs(dd);
 91         printf("%.6lf\n",dd);
 92 
 93         double t1 = ( (C-A)^p2 )*(p1^p2);
 94         t1 /= norm(p1^p2)*norm(p1^p2);
 95         double t2 =  ( (C-A)^p1 )*(p1^p2);
 96         t2 /= norm(p1^p2)*norm(p1^p2);
 97         mid1 = A + (p1 * t1);
 98         mid2 = C + (p2 * t2);
 99         mid1.output();
100         printf(" ");
101         mid2.output();
102         printf("\n");
103 
104     }
105     return 0;
106 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-09-15 23:10  kuangbin  阅读(1495)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: