题意:就是给你两条线段AB , CD ,一个人在AB上跑速度p, 在CD上跑q,在其他地方跑速度是r
问你从A到D最少的时间是多少
#include<stdio.h>
#include<string.h>
#include<math.h>
#define eps 1e-6
#define INF 9999999999.0
int P, Q, R;
struct point
{
double x, y;
}A, B, C, D;
point getMid(point a, point b)
{
point c;
c.x = (a.x + b.x) * 1.0 / 2;
c.y = (a.y + b.y) * 1.0 / 2;
return c;
}
double getDistance(double x1, double y1, double x2, double y2)
{
return sqrt((x1-x2)*(x1-x2)*1.0 + (y1-y2)*(y1-y2)*1.0);
}
double getTime(point a, point b, int v)
{
double dis = getDistance(a.x, a.y, b.x, b.y);
double t = dis / (v * 1.0);
return t;
}
double search1(point p)
{
double t1 = 0, t2 = 1;
point l = C, r = D, mid1, mid2;
while(fabs(t1 - t2) > eps)
{
mid1 = getMid(l, r);
mid2 = getMid(mid1, r);
t1 = getTime(p, mid1, R) + getTime(D, mid1, Q);
t2 = getTime(p, mid2, R) + getTime(D, mid2, Q);
if(t1 - t2 > eps)
l = mid1;
else if( t1 - t2 < -eps)
r = mid2;
else return t1;
}
return t1;
}
double search2()
{
double t1 = 0, t2 = 1;
point l, r, mid1, mid2;
l = A;
r = B;
while(fabs(t1 - t2) > eps)
{
mid1 = getMid(l, r);
mid2 = getMid(mid1, r);
t1 = getTime(A, mid1, P) + search1(mid1);
t2 = getTime(A, mid2, P) + search1(mid2);
if(t1 - t2 > eps)
l = mid1;
else if( t1 - t2 < -eps)
r = mid2;
else return t1;
}
return t1;
}
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("%d%d%d", &P, &Q, &R);
printf("%.2lf\n", search2());
}
return 0;
}