题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3400
题意:
给定两条平行的路的左右端点的坐标。然后给定三个速度求表示在分别在两条路上的速度和不在路上走的速度
求从点A走到点D的最短时间。
分析:
先假定一个点固定的话 那么中间肯定有一个点可以使时间最小 然后是一个下凸型函数,
我们可以用三分来求。但是本题的两个点都不固定,那么思考一下 我们就可以尝试用
两个三分来做,一个三分求一条路上的点,另一个求另外一条路上的点。
注意求距离开方的时候会有精度损失。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double eps = 1e-7;
const double ee = 1e-9;
double p,q,r;
struct point{
double x,y;
};
point a,b,c,d,tmp1,tmp2;
double dis(point a,point b)
{
return sqrt(ee+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double calu(double t)
{
tmp2.x=d.x+(c.x-d.x)/dis(c,d)*t*q;
tmp2.y=d.y+(c.y-d.y)/dis(c,d)*t*q;
return t + dis(tmp1,tmp2)/r;
}
double sanfen(double t)
{
tmp1.x=a.x+(b.x-a.x)/dis(a,b)*t*p;
tmp1.y=a.y+(b.y-a.y)/dis(a,b)*t*p;
double low=0,high=dis(c,d)/p,ll,rr;
while(high-low>eps){
ll=(low*2+high)/3;
rr=(low+high*2)/3;
if(calu(ll)<calu(rr)) high=rr;
else low=ll;
}
return t+calu(ll);
}
int main()
{
int t;
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("%lf%lf%lf",&p,&q,&r);
double low = 0, high = dis(a,b)/p,ll,rr;
while(high-low>eps){
ll=(low*2+high)/3;
rr=(low+high*2)/3;
if(sanfen(ll)<sanfen(rr)) high=rr;
else low = ll;
}
printf("%.2lf\n",sanfen(ll));
}
return 0;
}