Line belt(HDU 3400)(三分套三分)

思路瞅的某dalao的这篇博客

https://www.cnblogs.com/cjjsb/p/9179699.html

/*
三分套三分:AB边存在E点,CD边存在F点,假设E点已经在了,只需确定F点的话,可以知道F点在CD边上移动是一个凹形函数,F点已知,确定E点的话
E点在AB上移动其距离函数形成的曲线是一个直线,直线+凹形函数也是凹形函数,所以可以用三分做。在三分确定E的时候,假装E点已经确定了,再E点之内
嵌套三分求F点。大体思路就是这样吧。。呜呜呜呜呜呜~~半吊子工科生的用词不专业,菜哭辽~
*/
#include
#include
#include
#include
#include
const double epp = 1e-6;
using namespace std;

int t,P,Q,R;
struct node{
double x;
double y;
}a,b,c,d;

double dis(node a,node b){
return sqrt((b.x-a.x)(b.x-a.x) + (b.y-a.y)(b.y-a.y));
}

double get2(double p1,double p2){
node X,Y;
X.x = a.x + p1*(b.x-a.x); X.y = a.y + p1*(b.y-a.y);
Y.x = c.x + p2*(d.x-c.x); Y.y = c.y + p2*(d.y-c.y);
return dis(a,X)/P + dis(X,Y)/R + dis(Y,d)/Q;
}

double get1(double x){
double l = 0.0, r = 1.0;
while(r-l > epp){
double lmid = l + (r-l)/3.0, rmid = r - (r-l)/3.0;
if(get2(x,lmid) < get2(x,rmid)) r = rmid;
else l = lmid;
}
return get2(x,l);
}

int main(){
while(~scanf("%d",&t)){
while(t–){
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("%d%d%d",&P,&Q,&R);
double l = 0.0, r = 1.0;
while(r-l > epp){
double lmid = l + (r-l)/3.0, rmid = r - (r-l)/3.0;
if(get1(lmid) < get1(rmid)) r = rmid;
else l = lmid;
}
printf("%.2f\n",get1(l));
}
}
return 0;
}

你可能感兴趣的:(Line belt(HDU 3400)(三分套三分))