湘潭邀请赛Hurry Up

Problem Description

GG is some what afraid of his MM. Once his MM asks, he will always try his best to rush to their home. Obvious, he can run home in straight line directly.  Alternatively, he can run to the main road and call the taxi. You can assume there is only one main road on the x-axis, with unlimited length. Given the initial location of GG and his destination,  please help to ask the minimize time to get home. GG will always run at the fixed speed of vr, and the taxi can move at the fixed speed of vt You can also assume that, only GG reach the main road, he can catch the taxi immediately. And the taxi will go towards home ( not necessay along the road ). Bisides, GG can run arbitrary length, and pay arbitrarily for the taxi.
P.S. MM: abbr. Ma Ma

Input

Multiple test cases. First line, an integer T ( 1 ≤ T ≤ 2000 ), indicating the number of test cases. For each test cases, there are 6 integers x1, y1, x2, y2, vr, vt in a line. ( -1000 <= x1, y1, x2, y2 <= 1000, 1 <= vr < vt <= 1000 ) (x1, y1) : the initial location of GG (x2, y2) : the destination location of GG vr: GG's run speed  vt: taxi's speed

Ouput

For each test case, output a real number with 2 digits after the arithmetic point. It is the shorest time for GG to reach home.

Sample Input

2
1 1 2 2 1 2
1 1 2 2 1 7

Sample Output

1.41
1.32

题意:一个人从一个点到另一个点,可以选择全程走路,也可以选择先走一段在坐车,求最短时间。车子在x轴上
题解:对区间进行三分,这道题wrong了n次。最后计算的是如果是算midwrong,算l就直接ac。究竟为什么还没想通

#include<stdio.h>

#include<math.h>

#include<iostream>

#include<algorithm>

using namespace std;

double x1,x2,ll1,ll2,sw,sc;

double fan(double x)

{    

return sqrt((x-x1)*(x-x1)+ll1*ll1)/sw+sqrt((x-x2)*(x-x2)+ll2*ll2)/sc;

}

int main()

{  

int n,i,p=0;    

scanf("%d",&n);    

for(i=0;i<n;i++)    

{        

scanf("%lf%lf%lf%lf%lf%lf",&x1,&ll1,&x2,&ll2,&sw,&sc);

        if(x1>x2) swap(x1,x2);        

double l,r,s,mid1,mid2;         l=x1;         r=x2;         s=sqrt((x1-x2)*(x1-x2)+(ll1-ll2)*(ll1-ll2))/sw;   

      while(r-l>1e-10)     

    {             mid1=(l+r)/2;            

mid2=(mid1+r)/2;            

if(fan(mid1)<fan(mid2))  r=mid2;   

          else l=mid1;        

}        

printf("%.2lf\n",s<fan(l)?s:fan(l));  //如果l换成mid1就wrong了。

   }   

  return 0;

}

posted @ 2013-06-26 16:40  forevermemory  阅读(275)  评论(0编辑  收藏  举报