HDU 3400 Line belt

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3400

 

题目大意:有两个跑道,ab和cd,xh在ab跑道上面的速度为p,在cd跑道上面的速度为q,在其他地方的速度为r。问xh从a到d最少时间。

分析:最多可有三段,先三分ab,固定后在三分cd,取最小时间。

三分+三分。

 

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define eps 1e-6
struct point
{
    double x,y;
}a,b,c,d,aa,dd;
double ab,cd;
double p,q,r;

double dis(point a,point b)
{
    return sqrt(eps+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));//这个地方一定要加eps,加大精度,可能测试数据都是int类型开方有误差
}

double test(double k)
{
    dd.x=d.x+(c.x-d.x)/cd*k*q;
    dd.y=d.y+(c.y-d.y)/cd*k*q;
    return k+dis(aa,dd)/r;//dd点到d点的时间 + aa点到dd点时间
}

double sanfen(double k)
{
    aa.x=a.x+(b.x-a.x)/ab*k*p;
    aa.y=a.y+(b.y-a.y)/ab*k*p;
    double low=0,high=cd/q,mid,midmid;
    while(high-low>eps)//第二次二分
    {
        mid=(low+high)/2;
        midmid=(mid+high)/2;
        if(test(mid)<test(midmid))
            high=midmid;
        else
            low=mid;
    }
    return k+test(mid);//上面所求时间 + a点到aa点的时间
}

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 low=0,high=ab/p,mid,midmid;
        while(high-low>eps)//第一次二分
        {
            mid=(low+high)/2;
            midmid=(mid+high)/2;
            if(sanfen(mid)<sanfen(midmid))
                high=midmid;
            else
                low=mid;
        }
        printf("%.2f\n",sanfen(mid));
    }
    return 520;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值