三分——Line belt

来源:互联网 发布:i started a joke 知乎 编辑:程序博客网 时间:2024/06/03 16:02
In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww's speed on AB is P and on CD is Q, he can move with the speed R on other area on the plane. 
How long must he take to travel from A to D?
Input
The first line is the case number T. 
For each case, there are three lines. 
The first line, four integers, the coordinates of A and B: Ax Ay Bx By. 
The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy. 
The third line, three integers, P Q R. 
0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000 
1<=P,Q,R<=10
Output
The minimum time to travel from A to D, round to two decimals.
Sample Input
10 0 0 100100 0 100 1002 2 1
Sample Output

136.60



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-8;
struct Point
{
    double x,y;
}a,b,c,d,bb,dd;
double ab,cd,p,q,r;
double dis(Point u,Point v)
{
    return sqrt(eps+(u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));
}
double work(double t)
{
    dd.x=d.x+(c.x-d.x)/cd*t*q;
    dd.y=d.y+(c.y-d.y)/cd*t*q;
    return t+dis(bb,dd)/r;
}
double solve(double t)
{
    bb.x=a.x+(b.x-a.x)/ab*t*p;
    bb.y=a.y+(b.y-a.y)/ab*t*p;
    double l=0,h=cd/q,mid1,mid2;
    while(h-l>eps)
    {
        mid1=(l*2+h)/3;
        mid2=(h*2+l)/3;
        if(work(mid1)<work(mid2))
        h=mid2;
        else
        l=mid1;
    }
    return t+work(l);
}
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 l=0,h=ab/p,mid1,mid2;
        while(h-l>eps)
        {
            mid1=(l*2+h)/3;
            mid2=(h*2+l)/3;
            if(solve(mid1)<solve(mid2))
            h=mid2;
            else
            l=mid1;
        }
        printf("%.2f\n",solve(mid1));
    }
    return 0;
}

这个是正确代码,但是如果dis哪里不加eps就是wa,我也是懵啊。哎西吧!

0 0