Hdu3400 line belt (double three points)

Source: Internet
Author: User

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 ;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.