Question:Two parallel line segments AB and Cd are given, and then a person starts from point A of the online line segment AB to point D. The speed of the person's online line segment AB is P, the speed on the online segment CD is Q, and the speed in other places is R, asking for the shortest time from point A to point D.
Analysis: we have to use three points. Well, I didn't expect it. First we had to fix a point on AB, and then we got three points on CD, which is a nested three-point search with clear thinking,CodeIt's not hard to crack.
Attention: precision and boundary Processing
View code
# Include <iostream>
# Include <algorithm>
# Include <math. h>
Using Namespace STD;
Struct Point
{
Double X, Y;
};
Point A, B, C, D;
Int P, Q, R;
Double Length (point X, point y)
{
Return SQRT (X. x-Y.x) * (X. x-Y.x) + (X. y-Y.y) * (X. y-Y.y ));
}
Double Time ( Double A, Double B)
{
Point X, Y;
X. x = A * (B. x-A.x) + A. X;
X. y = a * (B. y-A.y) + A. Y;
Y. x = B * (C. x-D.x) + d. X;
Y. Y = B * (C. y-D.y) + d. Y;
Return Length (A, x)/P + Length (D, Y)/q + Length (x, y)/R;
}
Double Thidiv ( Double Lena)
{
Double Left = 0.0 , Right = 1.0 , Lm, Rm;
While (Right-left> 1E- 6 )
{
Lm = (left * 2.0 + Right )/ 3.0 ;
Rm = (left + right * 2.0 )/ 3.0 ;
If (Time (Lena, lm) <= Time (Lena, RM ))
Right = Rm;
Else Left = lm;
}
Return Time (Lena, left );
}
Int Main ()
{
Int T;
Scanf ( " % D " , & T );
While (T --)
{
Scanf ( " % Lf " , & A. X, & A. Y, & B. X, & B. Y );
Scanf ( " % Lf " , & C. X, & C. Y, & D. X, & D. y );
Scanf ( " % D " , & P, & Q, & R );
Double Left = 0.0 , Right = 1.0 , Lm, Rm;
While (Right-left> 1E- 6 ) // The big Yu Code actually writes the minor Yu code, and the test example passes through, WA n times.
{
// Here, three points are the percentage of the distance taken on AB to AB.
Lm = (left * 2 + Right )/ 3.0 ;
Rm = (left + 2 * Right )/ 3.0 ;
If (Thidiv (LM) <= thidiv (RM ))
Right = Rm;
Else Left = lm;
}
Printf ( " %. 2f \ n " , Thidiv (left ));
}
Return 0 ;
}