原题:HDU- 3400
三分搜索用来找中间最优点,设变量l、r、mid=(l+r)/2、mmid=(mid+r)/2;一步步向最优值逼近,适用于二次函数模型
这个题以找AB的最优点来寻找CD的最优点,用两个三分搜索
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
double Ax,Ay,Bx,By,Cx,Cy,Dx,Dy;
double p,q,r;
double f4(double x1,double y1,double x2,double y2) {
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double f3(double xie,double x1,double y1) {
double x=Cx+(Dx-Cx)*xie;
double y=Cy+(Dy-Cy)*xie;
return f4(x,y,x1,y1)/r+f4(x,y,Dx,Dy)/q;
}
double f2(double xie) {
double x=Ax+(Bx-Ax)*xie;
double y=Ay+(By-Ay)*xie;
double l=0.0,r=1.0,mid,mmid,t1,t2;
while(r-l>1e-8) {
mid=(r+l)/2;
mmid=(mid+r)/2;
t1=f3(mid,x,y);
t2=f3(mmid,x,y);
if(t1<t2)
r=mmid;
else if(t1>t2)
l=mid;
else break;
}
return t1+f4(x,y,Ax,Ay)/p;
}
double f1() {
double l=0.0,r=1.0,mid,mmid,t1,t2;
while(r-l>1e-8) {
mid=(r+l)/2;
mmid=(mid+r)/2;
t1=f2(mid);
t2=f2(mmid);
if(t1<t2)
r=mmid;
else if(t1>t2)
l=mid;
else break;
}
return t1;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%lf %lf %lf %lf",&Ax,&Ay,&Bx,&By);
scanf("%lf %lf %lf %lf",&Cx,&Cy,&Dx,&Dy);
scanf("%lf %lf %lf",&p,&q,&r);
printf("%.2f\n",f1());
}
return 0;
}