Line belt(三分镶嵌)
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 1Sample Output
f = AE / p
g= EF / r + FD / q
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 #define EPS 1e-8 8 struct Point 9 { 10 double x; 11 double y; 12 } a, b, c, d, e, f; 13 double p, q, r; 14 double dis(Point p1, Point p2)///两点之间的距离 15 { 16 return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); 17 } 18 double calc(double alpha)///alpha代表在f点在cd中位置的比率 19 { 20 f.x = c.x + (d.x - c.x) * alpha; 21 f.y = c.y + (d.y - c.y) * alpha; 22 return dis(f, d) / q + dis(e, f) / r;///返回在ef和fd上花费的时间 23 } 24 double inter_tri(double alpha)///在f点进行三分 25 { 26 double l = 0.0, r = 1.0, mid, mmid, cost; 27 e.x = a.x + (b.x - a.x) * alpha; 28 e.y = a.y + (b.y - a.y) * alpha;///e点在线段ab中的位置 29 while (r - l > EPS) 30 { 31 mid = (l + r) / 2; 32 mmid = (mid + r) / 2; 33 cost = calc(mid); 34 if (cost <= calc(mmid)) 35 r = mmid; 36 else 37 l = mid; 38 } 39 return dis(a, e) / p + cost; 40 } 41 double solve()///在e点进行三分 42 { 43 double l = 0.0, r = 1.0, mid, mmid, ret; 44 while (r - l > EPS) 45 { 46 mid = (l + r) / 2; 47 mmid = (mid + r) / 2; 48 ret = inter_tri(mid); 49 if (ret <= inter_tri(mmid)) 50 r = mmid; 51 else 52 l = mid; 53 } 54 return ret; 55 } 56 int main() 57 { 58 int T; 59 double ans; 60 scanf("%d",&T); 61 while(T--) 62 { 63 scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y); 64 scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y); 65 scanf("%lf%lf%lf",&p,&q,&r); 66 ans=solve(); 67 printf("%.2lf\n",ans); 68 } 69 return 0; 70 }
本文作者:王陸
本文链接:https://www.cnblogs.com/wkfvawl/p/9346163.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步