问题描述
In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww’s speed on AB is P and on CD is Q, he can move with the speed R on other area on the plane.
How long must he take to travel from A to D?
输入
The first line is the case number T.
For each case, there are three lines.
The first line, four integers, the coordinates of A and B: Ax Ay Bx By.
The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy.
The third line, three integers, P Q R.
0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000
1<=P,Q,R<=10
1
0 0 0 100
100 0 100 100
2 2 1
输出
The minimum time to travel from A to D, round to two decimals.
136.60
解题思路
- 问题要求从A到D点所需的最短时间。显然在AB段必存在一点E,CD段存在一点F,使得从A到D时间最短。
- 先用三分法确定一个E点,然后E点可看作一个固定点。
- 随后用三分法确定F点,算出最短时间。
c++代码
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double bias = 1e-8;
int Ax, Ay, Bx, By, Cx, Cy, Dx, Dy;
int speedAB, speedOther, speedCD;
double getCDPoint(double X, double Y){
double startX = Cx, startY = Cy, endX = Dx, endY = Dy, mid1X, mid1Y, mid2X, mid2Y;
double time1 = 0, time2 = 0;
bool flag = true;
while(flag){
mid1X = startX + (endX - startX) / 3;
mid1Y = startY + (endY - startY) / 3;
mid2X = endX - (endX - startX) / 3;
mid2Y = endY - (endY - startY) /3;
time1 = (sqrt(pow((Dx - mid1X), 2) + pow((Dy - mid1Y), 2)) / speedCD) + (sqrt(pow((X - mid1X), 2) + pow((Y - mid1Y), 2)) / speedOther);
time2 = (sqrt(pow((Dx - mid2X), 2) + pow((Dy - mid2Y), 2)) / speedCD) + (sqrt(pow((X - mid2X), 2) + pow((Y - mid2Y), 2)) / speedOther);
if(time1 < time2){
endX = mid2X;
endY = mid2Y;
}
else{
startX = mid1X;
startY = mid1Y;
}
if(abs(time1 - time2) <= bias){
flag = false;
}
}
return time1;
}
double getABPoint(){
double startX = Ax, startY = Ay, endX = Bx, endY = By, mid1X, mid1Y, mid2X, mid2Y;
double time1 = 0, time2 = 0;
bool flag = true;
while(flag){
mid1X = startX + (endX - startX) / 3;
mid1Y = startY + (endY - startY) / 3;
mid2X = endX - (endX - startX) / 3;
mid2Y = endY - (endY - startY) /3;
time1 = sqrt(pow((mid1X - Ax), 2) + pow((mid1Y - Ay), 2)) / speedAB + getCDPoint(mid1X, mid1Y);
time2 = sqrt(pow((mid2X - Ax), 2) + pow((mid2Y - Ay), 2)) / speedAB + getCDPoint(mid2X, mid2Y);
if(time1 < time2){
endX = mid2X;
endY = mid2Y;
}
else{
startX = mid1X;
startY = mid1Y;
}
if(abs(time1 - time2) <= bias){
flag = false;
}
}
return time1;
}
int main(){
int numCases;
cin >> numCases;
while (numCases--)
{
cin >> Ax >> Ay >> Bx >> By >> Cx >> Cy >> Dx >> Dy;
cin >> speedAB >> speedCD >> speedOther;
cout << fixed << setprecision(2) << getABPoint() << endl;
}
return 0;
}