Two parallel lines, AB, CD, and AB, are given. The speed of CD is Q, and the line segment is R. How long is the shortest time from A to D?
Method: 3 points. After a simple analysis, we found that this function is a single-peak function, which is first reduced and then increased. It is applicable to three points;
Implementation: first, 3-points for AB, nested with 3-points for CD;
#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;#define LL long long#define PI acos(-1.0)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-10#define FRE freopen("a.txt","r",stdin)#define N 1000struct point{ double x,y;};double p,q,r;double dis(point a,point b){ return sqrt( (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y) );}double cal_cd(point c,point d,point mm){ int i,j; point ll,rr,mid,midmid; ll=c; rr=d; double t1,t2; do{ mid.x=(ll.x+rr.x)/2.0; mid.y=(ll.y+rr.y)/2.0; midmid.x=(mid.x+rr.x)/2.0; midmid.y=(mid.y+rr.y)/2.0; t1=dis(d,mid)/q+dis(mid,mm)/r; t2=dis(d,midmid)/q+dis(midmid,mm)/r; if(t1<t2) rr=midmid; else ll=mid; }while(fabs(t1-t2)>=eps); return t1;}double cal_ab(point a,point b,point c,point d){ int i,j; point ll,rr,mid,midmid; ll=a; rr=b; double t1,t2; do{ mid.x=(ll.x+rr.x)/2.0; mid.y=(ll.y+rr.y)/2.0; midmid.x=(mid.x+rr.x)/2.0; midmid.y=(mid.y+rr.y)/2.0; double tmp1=cal_cd(c,d,mid); double tmp2=cal_cd(c,d,midmid); t1=dis(a,mid)/p+tmp1; t2=dis(a,midmid)/p+tmp2; if(t1<t2) rr=midmid; else ll=mid; }while(fabs(t1-t2)>=eps); return t1;}int main(){ int t; scanf("%d",&t); while(t--){ point a,b,c,d; scanf("%lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y); scanf("%lf %lf %lf %lf",&c.x,&c.y,&d.x,&d.y); scanf("%lf %lf %lf",&p,&q,&r); double ans=cal_ab(a,b,c,d); printf("%.2f\n",ans); } return 0;}