HDU 3400 Line belt
Line belt
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3925 Accepted Submission(s): 1524
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.
How long must he take to travel from A to D?
How long must he take to travel from A to D?
Input
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
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
Output
The minimum time to travel from A to D, round to two decimals.
Sample Input
1
0 0 0 100
100 0 100 100
2 2 1
Sample Output
136.60
三分法求解问题,奇怪的是 do{}while();过, while(){}不过;
AC代码:
1 # include <bits/stdc++.h> 2 using namespace std; 3 const double eps = 1e-5; 4 struct Point 5 { 6 double x; 7 double y; 8 }; 9 10 double dis(Point a, Point b) 11 { 12 return sqrt((b.y - a.y)*(b.y - a.y) + (a.x - b.x)*(a.x - b.x)); 13 } 14 15 double p, q, r; 16 double re2(Point a, Point c, Point d) 17 { 18 Point left, right; 19 Point mid, midmid; 20 double t1, t2; 21 left = c; right = d; 22 do 23 { 24 mid.x = (left.x + right.x) / 2; 25 mid.y = (left.y + right.y) / 2; 26 midmid.x = (mid.x + right.x) / 2; 27 midmid.y = (mid.y + right.y) / 2; 28 t1 = dis(a, mid) / r + dis(mid, d) / q; 29 t2 = dis(a, midmid) / r + dis(midmid, d) / q; 30 if(t1 > t2) 31 left = mid; 32 else 33 right = midmid; 34 } 35 while(dis(left, right) >= eps); 36 return t1; 37 } 38 double re(Point a, Point b, Point c, Point d) 39 { 40 Point l, r; 41 Point mid, midmid; 42 double t1, t2; 43 l = a; 44 r = b; 45 do 46 { 47 mid.x = (l.x + r.x) / 2; 48 mid.y = (l.y + r.y) / 2; 49 midmid.x = (mid.x + r.x) / 2; 50 midmid.y = (mid.y + r.y) / 2; 51 t1 = dis(a, mid) / p + re2(mid, c, d); 52 t2 = dis(a, midmid) / p + re2(midmid, c, d); 53 if(t1 > t2) 54 l = mid; 55 else 56 r = midmid; 57 }while(dis(l, r) >= eps); 58 return t1; 59 } 60 61 62 int main() 63 { 64 int T; 65 scanf("%d", &T); 66 while(T--) 67 { 68 Point a, b, c, d; 69 scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); 70 scanf("%lf%lf%lf%lf", &c.x, &c.y, &d.x, &d.y); 71 scanf("%lf%lf%lf", &p, &q, &r); 72 73 printf("%.2lf\n", re(a, b, c, d)); 74 } 75 76 77 return 0; 78 }
生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。