Line Belt(HDU-3400)

Problem Description

    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.

    Technical Specification:

        1) 0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000

        2) 1<=P,Q,R<=10

Output

     The minimum time to travel from A to D, round to two decimals.

Sample Input

    1

    0 0 0 100
    100 0 100 100
    2 2 1

Sample Output

    136.60

思路:

设AB所在直线为x,CD所在直线为y,E、F分别是AB、CD上两动点,EF所在直线为z

则:

    人在线段AB上花的时间为:time1=AE/p

        人走完Z和Y所花的时间为:time2=EF/r+FD/q

        总时间:time=time1+time2

        因此,先三分AB上的点,找到E,再三分CD上的点找到F,利用上述公式计算出最短时间即可

Source Program

#include<cstdio>  
#include<cmath>
double Ax,Ay,Bx,By,Cx,Cy,Dx,Dy;
double Ex,Ey,Fx,Fy;
double p,q,r;
inline double d(double a,double b,double c,double d);//计算两点距离的函数
double calculate_E(double k);//求time的函数
double calculate_F(double k);//求time2的函数

int main()
{
    int t;
    double left,right,mid,midmid;
    
    scanf("%d",&t);//输入测试数据个数
    while(t--)
    {  
        scanf("%lf %lf %lf %lf",&Ax,&Ay,&Bx,&By);//输入A、B的横纵坐标
        scanf("%lf %lf %lf %lf",&Cx,&Cy,&Dx,&Dy);//输入C、D的横纵坐标
        scanf("%lf %lf %lf",&p,&q,&r);//输入在AB上的速度p,在CD上的速度q,在平面其它地方的速度r
        
        /*三分找点E*/
        left=0,right=1;//设置初值
        while(right-left>1e-10)
        {
            mid=(right+left)/2;//设置中值
            midmid=(mid+right)/2;//设置中值
            if(calculate_E(mid)<calculate_E(midmid))
                right=midmid;
            else
                left=mid;
        }  
        printf("%.2f\n",calculate_E(mid));//输出最短时间
    }
    return 0;  
}
double d(double a,double b,double c,double d)//计算两点距离的内联函数
{
    return sqrt( (c-a)*(c-a) + (d-b)*(d-b) );
}
double calculate_E(double k)//求time的函数
{
    double left,right,mid,midmid;
    double time1,time2,time;
    
    Ex=Ax+(Bx-Ax)*k;//求E的横坐标
    Ey=Ay+(By-Ay)*k;//求E的纵坐标
    
    /*三分找点F*/
    left=0;right=1;//设置初值
    while(right-left>1e-10)
    {
        mid=(right+left)/2;//设置中值
        midmid=(mid+right)/2;//设置中值
        if(calculate_F(mid)<calculate_F(midmid))
                right=midmid;
        else
                left=mid;
    }
    time1=d(Ax,Ay,Ex,Ey)/p;
    time2=calculate_F(mid);
    time=time1+time2;
    return time;
}  

double calculate_F(double k)//求time2的函数
{
    double time2;
    
    Fx=Cx+(Dx-Cx)*k;//求F的横坐标
    Fy=Cy+(Dy-Cy)*k;//求F的纵坐标
    
    time2=d(Dx,Dy,Fx,Fy)/q+d(Ex,Ey,Fx,Fy)/r;
    return time2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值