题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3400
这个题目一看就知道是二分枚举之类的,但是一次是搞定不了了,要来两次
三分枚举ab上的点,然后求这个点到d的距离,这里同样是三分枚举,这两个同时都能满足三分枚举
的条件,那么结合起来就OK了!
表面上看是两层的三分枚举,不过两层的思想都是一样的!
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define inf 1e12
#define eps 1e-8
struct point{
double x,y;
point(double _x=0,double _y=0):x(_x),y(_y){}
}a,b,c,d,dir1,dir2;
double v_a,v_b,v_c;
point sub(point &a,point &b){
return point(b.x-a.x,b.y-a.y);
}
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 find_ans(point p){//计算从P点到D点的最短时间
double ans=inf,now,then;
double l=0,r=1,mid;
point temp;
while(fabs(l-r)>eps){
mid=(l+r)/2;
temp=point(c.x+dir2.x*mid,c.y+dir2.y*mid);
now=dis(p,temp)/v_c+dis(temp,d)/v_b;
mid+=eps;
temp=point(c.x+dir2.x*mid,c.y+dir2.y*mid);
then=dis(p,temp)/v_c+dis(temp,d)/v_b;
mid-=eps;
if(then > now) r=mid-eps;
else l=mid+eps;
if(ans > now)
ans=now;
}
return ans;
}
double solve(){
double ans=inf,now,then,l=0,r=1,mid;
point temp;
while(fabs(l-r)>eps){
mid=(l+r)/2;
temp=point(a.x+mid*dir1.x,a.y+mid*dir1.y);
now=find_ans(temp)+dis(a,temp)/v_a;
mid+=eps;
temp=point(a.x+mid*dir1.x,a.y+mid*dir1.y);
then=find_ans(temp)+dis(a,temp)/v_a;
mid-=eps;
if(then >= now) r=mid-eps;
else l=mid+eps;
if(ans > now)
ans=now;
}
return ans;
}
int main(){
int i,j,k,t;
scanf("%d",&t);
while(t--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y,&v_a,&v_b,&v_c);
dir1=sub(a,b);
dir2=sub(c,d);
printf("%.2lf\n",solve());
}
return 0;
}