HDU3400 Line belt (几何+三分)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3400


题意:

给定两条平行的路的左右端点的坐标。然后给定三个速度求表示在分别在两条路上的速度和不在路上走的速度

求从点A走到点D的最短时间。


分析:

先假定一个点固定的话 那么中间肯定有一个点可以使时间最小 然后是一个下凸型函数,

我们可以用三分来求。但是本题的两个点都不固定,那么思考一下 我们就可以尝试用

两个三分来做,一个三分求一条路上的点,另一个求另外一条路上的点。

注意求距离开方的时候会有精度损失。


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const double eps = 1e-7;
const double ee = 1e-9;

double p,q,r;

struct point{
    double x,y;
};

point a,b,c,d,tmp1,tmp2;

double dis(point a,point b)
{
    return sqrt(ee+(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double calu(double t)
{
    tmp2.x=d.x+(c.x-d.x)/dis(c,d)*t*q;
    tmp2.y=d.y+(c.y-d.y)/dis(c,d)*t*q;
    return t + dis(tmp1,tmp2)/r;
}
double sanfen(double t)
{
    tmp1.x=a.x+(b.x-a.x)/dis(a,b)*t*p;
    tmp1.y=a.y+(b.y-a.y)/dis(a,b)*t*p;
    double low=0,high=dis(c,d)/p,ll,rr;
    while(high-low>eps){
        ll=(low*2+high)/3;
        rr=(low+high*2)/3;
        if(calu(ll)<calu(rr)) high=rr;
        else low=ll;
    }
    return t+calu(ll);
}

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("%lf%lf%lf",&p,&q,&r);
        double low = 0, high = dis(a,b)/p,ll,rr;
        while(high-low>eps){
            ll=(low*2+high)/3;
            rr=(low+high*2)/3;
            if(sanfen(ll)<sanfen(rr)) high=rr;
            else low = ll;
        }
        printf("%.2lf\n",sanfen(ll));
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值