题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3400
题目大意:有两个跑道,ab和cd,xh在ab跑道上面的速度为p,在cd跑道上面的速度为q,在其他地方的速度为r。问xh从a到d最少时间。
分析:最多可有三段,先三分ab,固定后在三分cd,取最小时间。
三分+三分。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define eps 1e-6
struct point
{
double x,y;
}a,b,c,d,aa,dd;
double ab,cd;
double p,q,r;
double dis(point a,point b)
{
return sqrt(eps+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));//这个地方一定要加eps,加大精度,可能测试数据都是int类型开方有误差
}
double test(double k)
{
dd.x=d.x+(c.x-d.x)/cd*k*q;
dd.y=d.y+(c.y-d.y)/cd*k*q;
return k+dis(aa,dd)/r;//dd点到d点的时间 + aa点到dd点时间
}
double sanfen(double k)
{
aa.x=a.x+(b.x-a.x)/ab*k*p;
aa.y=a.y+(b.y-a.y)/ab*k*p;
double low=0,high=cd/q,mid,midmid;
while(high-low>eps)//第二次二分
{
mid=(low+high)/2;
midmid=(mid+high)/2;
if(test(mid)<test(midmid))
high=midmid;
else
low=mid;
}
return k+test(mid);//上面所求时间 + a点到aa点的时间
}
int main()
{
int t;
cin>>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);
ab=dis(a,b);
cd=dis(c,d);
double low=0,high=ab/p,mid,midmid;
while(high-low>eps)//第一次二分
{
mid=(low+high)/2;
midmid=(mid+high)/2;
if(sanfen(mid)<sanfen(midmid))
high=midmid;
else
low=mid;
}
printf("%.2f\n",sanfen(mid));
}
return 520;
}