課程練習二-1006-Line belt

Problem Description
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.<br>How long must he take to travel from A to D?
 

Input
The first line is the case number T.<br>For each case, there are three lines.<br>The first line, four integers, the coordinates of A and B: Ax Ay Bx By.<br>The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy.<br>The third line, three integers, P Q R.<br>0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000<br>1<=P,Q,R<=10
 

Output
The minimum time to travel from A to D, round to two decimals.
 

Sample Input
1<br>0 0 0 100<br>100 0 100 100<br>2 2 1
 

Sample Output
136.60
 
題意:在AB上走速度爲P,在CD上走速度爲Q,在其他地方上走速度爲R。

求從A到D的最短時間。


思路:(看圖)

各兩個三分同時(嵌套)使用。

槽!剛開始圖沒傳上,csdn真難用!

AC代碼:

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<string.h>
#include<cmath>
using namespace std;
int P=0, Q=0, R=0;


struct point
{
double x;
double y;
};
double Distance(point a,point b)
{
return sqrt(fabs(pow((a.x - b.x), 2) + pow((a.y - b.y), 2)));
}
double TTime(point a,point b,point c)  //第一次三分
{
point left, right, mid, midmid;
left.x = a.x; left.y = a.y;
right.x = b.x; right.y = b.y;
double ans;
while (1)
{
mid.x = (left.x + right.x) / 2;
mid.y = (left.y + right.y) / 2;
midmid.x = (mid.x + right.x) / 2;
midmid.y = (mid.y + right.y) / 2;
double n1 = Distance(midmid, a) / P + Distance(midmid, c) / R;
double n2 = Distance(mid, a) / P + Distance(mid, c) / R;
if (n1-n2 > 0)
{
right.x = midmid.x;
right.y = midmid.y;
}
else
{
left.x = mid.x;
left.y = mid.y;
}
if (fabs(n1-n2) < 0.0001)
{
ans = n1;
break;
}
}
return ans;
}
double calc_time(point a,point b,point c,point d)  //第二次三分
{
point left, right, mid, midmid;
left.x = c.x; left.y = c.y;
right.x = d.x; right.y = d.y;
double ans;
while (1)
{
mid.x = (left.x + right.x) / 2;
mid.y = (left.y + right.y) / 2;
midmid.x = (mid.x + right.x) / 2;
midmid.y = (mid.y + right.y) / 2;
double n1 = (Distance(midmid, d) / Q + TTime(a, b, midmid));
double n2 = Distance(mid, d) / Q + TTime(a, b, mid);
if (n1-n2>0)
{
right.x = midmid.x;
right.y = midmid.y;
}
else
{
left.x = mid.x;
left.y = mid.y;
}


if (fabs(n1-n2) < 0.0001)
{
ans = n1;
break;
}
}
return ans;
}
int main()
{
int T;
cin >> T;
while (T--)
{
double Ax, Bx, Ay, By;
double Cx, Cy, Dx, Dy;
cin >> Ax >> Ay >> Bx >> By;
cin >> Cx >> Cy >> Dx >> Dy;
int p, q, r;
cin >> p >> q >> r;
P = p; Q = q; R = r;
point a, b, c, d;
a.x = Ax; a.y = Ay;
b.x = Bx; b.y = By;
c.x = Cx; c.y = Cy;
d.x = Dx; d.y = Dy;
cout <<setprecision(2)<<fixed<< calc_time(a,b,c,d) << endl;
}
system("pause");
return 0;
}


感想:這道題不錯。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章