题意:没什么可谈的,就是一个几何+三分查找题,列出关系式,在x1到x2之间找出最小值,该函数是一个凸函数,利用三分查找,是最合适不过了。
要注意精度。
代码:
#include<stdio.h>
#include<math.h>
#define eps 1e-8
double v1,v2,low,high;
int sign(double x)
{
return x < -eps ? -1 : x > eps;
}
double count(double a,double b,double c,double d)
{
return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
double search(double x1,double y1,double x2,double y2 )
{
double mid1,mid2,min;
while(sign(high - low) >=0) //eps代表精度
{
mid1=(low+high)/2;
mid2=(mid1+high)/2;
double L=count(x1,y1,mid1,0)/v1+count(x2,y2,mid1,0)/v2;
double r=count(x1,y1,mid2,0)/v1+count(x2,y2,mid2,0)/v2;
if(sign(L-r) < 0)
{
high=mid2-eps;
min=L;
}
else
{
low=mid1+eps;
min=r;
}
}
return min;
}
int main()
{
int n;
double x1,x2,y1,y2;
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&v1,&v2);
double w=count(x1,y1,x2,y2)/v1;
low=x1<x2?x1:x2;
high=x2>x1?x2:x1;
double t=search(x1,y1,x2,y2);
if(w<t)
printf("%.2lf\n",w);
else
printf("%.2lf\n",t);
}
return 0;
}
/*
10
1 1 2 2 1 7
0 3 4 0 1 2
0 3 6 0 2 4
*/