hdu 3400 line belt (三分搜索)

#include<stdio.h>
#include<math.h>
#define eps 0.00000001
struct node
{
    double x,y;
}a,b,c,d;
double p,q,r;
void init()
{
    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 dist(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double scd(node p1,double m)
{
    node p2;
    p2.x=c.x+(d.x-c.x)*m;
    p2.y=c.y+(d.y-c.y)*m;
    return dist(p1,p2)/r+dist(d,p2)/q;
}
double solvecd(node p1)
{
    double l=0.0,r=1.0,mid,mmid,ans1,ans2;
    while(l+eps<r)
    {
        mid=(l+r)/2;
        mmid=(mid+r)/2;
        ans1=scd(p1,mid);
        ans2=scd(p1,mmid);
        if(ans1<ans2) r=mmid;
        else l=mid;
    }
    return scd(p1,l);
}
double sab(double m)
{
    node p1;
    p1.x=a.x+(b.x-a.x)*m;
    p1.y=a.y+(b.y-a.y)*m;
    return dist(a,p1)/p+solvecd(p1);
}
double solveab()
{
    double l=0.0,r=1.0,mid,mmid,ans1,ans2;
    while(l+eps<r)
    {
        mid=(l+r)/2;
        mmid=(mid+r)/2;
        ans1=sab(mid);
        ans2=sab(mmid);
        if(ans1<ans2) r=mmid;
        else l=mid;
    }
    return sab(l);
}
int main()
{
    int cs;scanf("%d",&cs);
    while(cs--)
    {
      init();
      double ans=solveab();
      printf("%.2f\n",ans);
    }
    return 0;
}
posted @ 2012-05-06 22:27  skyming  阅读(195)  评论(0编辑  收藏  举报