Line belt HDU - 3400嵌套三分

嵌套三分

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;
const double eps=1e-9;
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b))+eps;
}
Point a,b,c,d;
double p,q,r;


double f(Point t,double mid)
{
    Point t1;
    t1.x=d.x+(c.x-d.x)/dist(c,d)*mid*q;
    t1.y=d.y+(c.y-d.y)/dist(c,d)*mid*q;
    double len2=dist(t,t1);
    double t3=len2/r;
    return t3+mid;
}
double three_search_2(double mid)
{
    Point t;
    t.x=a.x+(b.x-a.x)/dist(a,b)*mid*p;
    t.y=a.y+(b.y-a.y)/dist(a,b)*mid*p;
    double l=0;
    double r=dist(c,d)/q;
    while(r-l>eps)
    {
        double lmid=l+(r-l)/3;
        double rmid=r-(r-l)/3;
        if(f(t,lmid)<=f(t,rmid))
        {
            r=rmid;
        }
        else
        {
            l=lmid;
        }
    }
    return mid+f(t,l);
}
double three_search_1(double l,double r)
{
    while(r-l>eps)
    {
        double lmid=l+(r-l)/3;
        double rmid=r-(r-l)/3;
        if(three_search_2(lmid)<=three_search_2(rmid))
        {
            r=rmid;
        }
        else
        {
            l=lmid;
        }
    }

    return three_search_2(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);
       double l=0;
       double r=dist(a,b)/p;
       double ans=three_search_1(l,r);
       printf("%.2lf\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值