There are two conveyor belts on a two-dimensional plane. Each conveyor belt can be regarded as a line segment. The two conveyor belts are segment AB and segment CD respectively. The moving speed of lxhgw on AB is P, on CD is Q, and on plane is R. Now lxhgw wants to go from point A to point D. He wants to know how long it takes to walk at least.
Finally, I dare say I will be three points.
This topic is a three-thirds set of classic examples
Just three points on two lines.
Pay attention to accuracy (don't be bombed - nan)
#include<bits/stdc++.h> using namespace std; const double eps=1e-4; struct Point{ double x,y; void read(){ cin>>x>>y; } }; double GetDis(Point A,Point B){ return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); } struct Line{ Point A,B; double dis; void read(){ A.read(); B.read(); dis=GetDis(A,B); } Point Get_Half(double len){ if(dis-len<eps)return B; if(len<eps)return A; double k=len/(dis); Point ret; ret.x=(B.x-A.x); ret.y=(B.y-A.y); ret.x*=k; ret.y*=k; ret.x+=A.x; ret.y+=A.y; return ret; } }L1,L2; double P,Q,R; double f(Point A,double sum){ Point S=L2.Get_Half(sum); return GetDis(A,S)/R+GetDis(S,L2.B)/Q; } double Get(double sum){ Point S=L1.Get_Half(sum); double l=0.00; double r=L2.dis; while(r-l>eps){ double rr=l+(r-l)/3.0*2; double ll=l+(r-l)/3.0; if(f(S,rr)<f(S,ll)){ l=ll; } else r=rr; } return f(S,l)+GetDis(L1.A,S)/P; } int main(){ // freopen("test.in","r",stdin); L1.read(); L2.read(); cin>>P>>Q>>R; double l=0.00; double r=L1.dis; while(r-l>eps){ double rr=l+(r-l)/3.0*2; double ll=l+(r-l)/3.0; if(Get(rr)<Get(ll)){ l=ll; } else r=rr; } cout<<fixed<<setprecision(2)<<Get(l); }