Line belt HDU - 3400嵌套三分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82693218

嵌套三分

#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;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82693218
今日推荐