Problem Description
GG is some what afraid of his MM. Once his MM asks, he will always try his best to rush to their home.
Obvious, he can run home in straight line directly. Alternatively, he can run to the main road and call the taxi.
You can assume there is only one main road on the x-axis, with unlimited length.
Given the initial location of GG and his destination, please help to ask the minimize time to get home.
GG will always run at the fixed speed of vr, and the taxi can move at the fixed speed of vt
You can also assume that, only GG reach the main road, he can catch the taxi immediately. And the taxi will go towards
Obvious, he can run home in straight line directly. Alternatively, he can run to the main road and call the taxi.
You can assume there is only one main road on the x-axis, with unlimited length.
Given the initial location of GG and his destination, please help to ask the minimize time to get home.
GG will always run at the fixed speed of vr, and the taxi can move at the fixed speed of vt
You can also assume that, only GG reach the main road, he can catch the taxi immediately. And the taxi will go towards
home ( not necessay along the road ).
Bisides, GG can run arbitrary length, and pay arbitrarily for the taxi.
Bisides, GG can run arbitrary length, and pay arbitrarily for the taxi.
Input
Multiple test cases. First line, an integer T(1<=T<=2000), indicating the number of test cases.
For each test cases, there are 6 integers x1, y1, x2, y2, vr, vt in a line.
( -1000 <= x1, y1, x2, y2 <= 1000, 1 <= vr < vt <= 1000 )
(x1, y1) : the initial location of GG
(x2, y2) : the destination location of GG
vr: GG's run speed
vt: taxi's speed
For each test cases, there are 6 integers x1, y1, x2, y2, vr, vt in a line.
( -1000 <= x1, y1, x2, y2 <= 1000, 1 <= vr < vt <= 1000 )
(x1, y1) : the initial location of GG
(x2, y2) : the destination location of GG
vr: GG's run speed
vt: taxi's speed
Output
For each test case, output a real number with 2 digits after the arithmetic point. It is the shorest time for GG to reach home.
Sample Input
2 1 1 2 2 1 2 1 1 2 2 1 7
Sample Output
1.411.32
/* 有两条路: 一是直接到达目的地(步行) 算出两点之间的距离 二是先到x轴再到目的地(步行到x轴,再打车到目的地) 用三分法,找出距离最短*/ #include<cstdio> #include<cmath> #include<iostream> using namespace std; double x11, x22, y11, y22, vr, vt; double fun(double x) { return (sqrt( pow((x11 - x), 2) + pow(y11, 2)) / vr + sqrt( pow((x22 - x), 2) + pow(y22, 2)) / vt); } int main() { int t; double sum1, sum2, x3, x4, f1, f2, x2, x1; while(scanf("%d", &t) != EOF) { while(t--) { scanf("%lf%lf%lf%lf%lf%lf", &x11, &y11, &x22, &y22, &vr, &vt); sum1 = sqrt(pow((x22 - x11), 2) + pow((y22 - y11), 2)) / vr;//直接算出两点之间的距离所花的时间 x1 = x11; x2 = x22; while(fabs(x2 - x1) > 1e-4)//三分查找 { x3 = (2 * x1 + x2) / 3; x4 = (x1 + x2 * 2) / 3; f1 = fun(x3); f2 = fun(x4); if(f1 > f2) x1 = x3; else x2 = x4; } sum2 = fun(x2); printf("%.2lf\n", sum1 > sum2 ? sum2 : sum1);//输出短的那个时间 } } return 0; }