hdu 3400 Line belt

来源:互联网 发布:vscode eclipse 编辑:程序博客网 时间:2024/05/17 18:45

Line belt

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3023    Accepted Submission(s): 1155


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

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

Output
The minimum time to travel from A to D, round to two decimals.
 

Sample Input
10 0 0 100100 0 100 1002 2 1
 

Sample Output
136.60
 

Author
lxhgww&&momodi
 

  1. 分析:基本思想就时枚举线段AB,CD上所有点的组合,取最优解,在次基础上
  2.         用三分优化,至于为何用三分,是因为它是凸函数,在此不予证明。
  3. 献上代码:
  4.  #include<stdio.h>
  5. #include<string.h>
  6. #include<algorithm>
  7. #include<iostream>
  8. #include<math.h>
  9. #define eps 1e-9
  10. using namespace std;
  11. double ax,ay,bx,by,cx,cy,dx,dy;
  12. double k1,l1,l2;
  13. double p,q,r1;
  14. double dis(double ax,double ay,double bx,double by)
  15. {
  16.     return sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay));
  17. }
  18. double cal(double k2)
  19. {
  20.     double t1,t2,t3;
  21.     t1=l1*k1/p;
  22.     t2=l2*(1-k2)/q;
  23.     t3=dis(ax+(bx-ax)*k1,ay+(by-ay)*k1,cx+(dx-cx)*k2,cy+(dy-cy)*k2)/r1;
  24.     return t1+t2+t3;
  25. }   
  26. double thr()
  27. {
  28.     double l,r,mid,midmid,dis1,dis2;
  29.     l=0,r=1;
  30.     while(r-l>=eps)
  31.     {   
  32.         mid=(l+r)/2;
  33.         midmid=(mid+r)/2;
  34.         dis1=cal(mid);
  35.         dis2=cal(midmid);
  36.         if(dis1<dis2)
  37.         {
  38.             r=midmid;
  39.         }
  40.         else
  41.         {
  42.             l=mid;
  43.         }
  44.     }
  45.     return dis1<dis2?dis1:dis2;
  46. }
  47. int main()
  48. {
  49.     int T;
  50.     double l,r,mid,midmid,dis1,dis2;
  51.     scanf("%d",&T);
  52.     while(T--)
  53.     {
  54.         scanf("%lf%lf%lf%lf",&ax,&ay,&bx,&by);
  55.         scanf("%lf%lf%lf%lf",&cx,&cy,&dx,&dy);
  56.         scanf("%lf%lf%lf",&p,&q,&r1);
  57.         l=0,r=1;
  58.         l1=dis(ax,ay,bx,by);
  59.         l2=dis(cx,cy,dx,dy);
  60.         while(r-l>=eps)
  61.         {   
  62.             mid=(l+r)/2;
  63.             midmid=(mid+r)/2;
  64.             k1=mid;
  65.             dis1=thr();
  66.             k1=midmid;
  67.             dis2=thr();
  68.             if(dis1<dis2)
  69.             {
  70.                 r=midmid;
  71.             }
  72.             else
  73.             {
  74.                 l=mid;
  75.             }
  76.         }
  77.         printf("%.2lf\n",dis1<dis2?dis1:dis2);
  78.     }
  79.     return 0;
  80. }
  81.         
  82.     
  83.      

0 0
原创粉丝点击