杭电 3400 Line belt 解题报告

    好玩的题。ABCD四点,三分法嵌套三分法。看代码应该更清晰一点~

#include<iostream>

#include<cmath>

using namespace std;



const double eps=1e-7;

double p,q,r;



struct Point

{

    double x,y;

} A,B,C,D;



double getDistance(Point a,Point b)

{

    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

}



double cToD(Point a)

{

    Point midl,midr;

    Point left=C,right=D;

    while(fabs(left.x-right.x)>eps||fabs(left.y-right.y)>eps)

    {

        midl.x=(right.x+left.x*2)/3;

        midl.y=(right.y+left.y*2)/3;

        midr.x=(right.x*2+left.x)/3;

        midr.y=(right.y*2+left.y)/3;

        if(getDistance(a,midl)/r+getDistance(D,midl)/q<getDistance(a,midr)/r+getDistance(D,midr)/q)

            right=midr;

        else

            left=midl;

    }

    return getDistance(a,left)/r+getDistance(D,left)/q;

}



double aToD()

{

    Point midl,midr;

    Point left=A,right=B;

    while(fabs(left.x-right.x)>eps||fabs(left.y-right.y)>eps)

    {

        midl.x=(right.x+left.x*2)/3;

        midl.y=(right.y+left.y*2)/3;

        midr.x=(right.x*2+left.x)/3;

        midr.y=(right.y*2+left.y)/3;

        if(getDistance(A,midl)/p+cToD(midl)<getDistance(A,midr)/p+cToD(midr))

            right=midr;

        else

            left=midl;

    }

    return getDistance(A,left)/p+cToD(left);

}



int main()

{

    int cas;

    cin>>cas;

    while(cas--)

    {

        cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y>>D.x>>D.y>>p>>q>>r;

        printf("%.2lf\n",aToD());

    }

}

 

你可能感兴趣的:(400)