三分再三分 HDU Problem F

Problem F

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :32768/32768K (Java/Other)

Total Submission(s) : 6   Accepted Submission(s) : 3

Problem Description

In a two-dimensional planethere are two line belts, there are two segments AB and CD, lxhgww's speed onAB is P and on CD is Q, he can move with the speed R on other area on theplane.<br>How long must he take to travel from A to D?

 

 

Input

The first line is the casenumber T.<br>For each case, there are three lines.<br>The firstline, four integers, the coordinates of A and B: Ax Ay Bx By.<br>Thesecond line , four integers, the coordinates of C and D:Cx Cy Dx Dy.<br>Thethird line, three integers, P Q R.<br>0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000<br>1<=P,Q,R<=10

 

 

Output

The minimum time to travelfrom A to D, round to two decimals.

 

 

Sample Input

1

0 0 0 100

100 0 100 100

2 2 1

 

 

Sample Output

136.60

 T算法分析:

三分在三分,现在AB上三分,找到E点,然后CD三分找到F点,这样二分枚举就能找到最小时间了。

代码实现:

 

#include<bits/stdc++.h>
using namespace std;
double ax,ay,bx,by,cx,cy,dx,dy,AB,CD;
double ex,ey,fx,fy;
double P,Q,R;
double len(double x1,double y1,double x2,double y2);
double cal(double amid);
double cal1(double mid);
int main()
{
    int T;
    cin>>T;
    while(T--)
    {

        cin>>ax>>ay>>bx>>by>>cx>>cy>>dx>>dy;
        cin>>P>>Q>>R;
        AB=len(ax,ay,bx,by);
        double high=AB,mid,midmid,low=0,m1,m2;//求AB中一点E使得时间最小
        while(fabs(high-low)>1e-9)
        {
         mid=(low+high)/2.0;
         midmid=(mid+high)/2.0;
         m1=cal(mid);

         m2=cal(midmid);
         if(m1>m2)
            low=mid;
         else
            high=midmid;
        }
           printf("%.2lf\n",cal(mid));
    }

    return 0;
}
double len(double x1,double y1,double x2,double y2)//求每段的距离
{

    return sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2)+1e-7);//jid加1e-7,防止开方结果比真实值小

}
double cal(double amid)   //求CD中一点F使得时间最小
{
   double t1=amid/P;        //AB时间

   ex=(bx-ax)*(amid/AB)+ax;//相似三角行求wE的横坐标
ey=(by-ay)*(amid/AB)+ay;
   CD=len(cx,cy,dx,dy);
   double high=CD,low=0,mid,midmid,m1,m2;
   while(fabs(high-low)>1e-9)
   {
       mid=(low+high)/2.0;
       midmid=(mid+high)/2.0;
       m1=cal1(mid);
       m2=cal1(midmid);
       if(m1>m2)
        low=mid;
       else
        high=midmid;
   }
  return t1+cal1(mid);
}
double cal1(double mid)//求EF和CD的时间
{
    fx=(dx-cx)*(mid/CD)+cx;
    fy=(dy-cy)*(mid/CD)+cy;
    double EF=len(ex,ey,fx,fy);
    double t2,t3;
    t2=EF/R;
    t3=(CD-mid)/Q;
    return t2+t3;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值