Topic link Pigs can ' t take a sudden turn
Time limit:1000 Ms Memory limit:65536 KB
problem Descrpition
You maybe has aced a question about computational Geometry,which describes the dogs ' journey and calculate the shortest D Istance between them. Now, I provide a easier problem about the pigs. Heir Path is half-lines. When they start running, they won ' t stop. Why? Because they can ' t take a sudden turn.
So, I provide the start points of both pigs and their velocities. Please tell me the shortest distance between them.
We purpose that both pigs start running at the same time and would not stop because they is running in an vast grasslane wi Thout any tree.
If they smash into each other, the answer is zero and ignore the influence. Input
The first line was an integer T which indicates the number of cases. For each case, first line was four numbers x1,y1,x2,y2, indicate the start points of the pigs (x1,y1), (X2,y2). The second line was four numbers u1,v1,u2,v2, indicate the velocities of the pigs (U1,V1), (U2,V2). T <= Absolute value of X1,Y1,X2,Y2,U1,V1,U2,V2 would not lager than 1000. Output
For each case, you should the output one line as "Case I:d". I stands for the case number and D stands for the answer, which should rounded to 6 decimal places. Sample Input
5
1 1 2 2
1 1 2 2 1
1 2 2 1
1-1-1
1 1 2 2
0 1 0-1
1 1 1 1
1 1 2 2
0 0 0 1
0 1 1 0
Sample Output
Case 1:1.414214 Case
2:0.000000 Case 3:1.000000 Case 4:0.000000 case
5:0.707107
Test Instructions
In a two-dimensional coordinate system, the starting position and velocity (vector representation) of two points are given, and the distance problem of the nearest two points is solved.
The relationship between two point coordinates and time is introduced by a known condition: A (x1+v1*t,y1+u1*t), B (x2+v2*t,y2+u2*t), and then the distance formula to get a distance with the time of two functions, and then two functions to find the minimum value of the problem, where the opening must be upward, A special sentence for a=0, which is equal to speed. Code
#include <cstdio> #include <iostream> #include <cmath> #define Y1 YY using
namespace Std;
const double eps=1e-8;
Double a,b,c;
Double x1,y1,u1,v1;
Double x2,y2,u2,v2;
Double len (Double t) {double ans=a*t*t+b*t+c;
return sqrt (ans);
} int main () {int t,ca=1;
scanf ("%d", &t);
while (t--) {scanf ("%lf%lf%lf%lf", &x1,&y1,&x2,&y2);
scanf ("%lf%lf%lf%lf", &u1,&v1,&u2,&v2);
c= (X1-X2) * (X1-X2) + (y1-y2) * (Y1-Y2);
b=2* (X1-X2) * (U1-U2) +2* (y1-y2) * (V1-V2);
A= (U1-U2) * (U1-U2) + (V1-V2) * (V1-V2);
printf ("Case%d:", ca++);
if (Fabs (a) <eps) {printf ("%.6f\n", Len (0));
Continue
} if (-b/(a*2) <0) {printf ("%.6f\n", Len (0));
} else {printf ("%.6f\n", Len (-b/(a*2)));
}} return 0; }