您的位置:首页 > 其它

hdoj3400Line belt【三分法】

2015-09-24 07:09 302 查看

Line belt

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3324    Accepted Submission(s): 1296


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

1
0 0 0 100
100 0 100 100
2 2 1

 

Sample Output

136.60

 

Author

lxhgww&&momodi
 

题意:在一条路AB上速度为P在CD上速度为q在其他地方速度为r问从A到B的最短时间

解题:思路在AB上三分确定转向CD的位置在CD上三分确定从AB来的进入道路哪一点算出最小时间即可

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define eps 1e-8
using namespace std;
int sgn(double n){
if(fabs(n)<eps)return 0;
else if(n<0)return -1;
return 1;
}
struct point{
double x,y;
}A,B,C,D;
double p,q,r;
double dist(point p1,point p2){
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
double Three2(point a){
point left=C,right=D;
point mid,mmid;
double t1,t2;
int size=100;
while(size--){
mid.x=(left.x+right.x)/2.0;
mid.y=(left.y+right.y)/2.0;
mmid.x=(mid.x+right.x)/2.0;
mmid.y=(mid.y+right.y)/2.0;
t1=dist(a,mid)/r+dist(mid,D)/q;
t2=dist(a,mmid)/r+dist(mmid,D)/q;
if(t1>t2)
left=mid;
else
right=mmid;
}
return t1;
}
double Three1(){
point left=A,right=B;
point mid,mmid;
double t1,t2;
int size=100;
while(size--){
mid.x=(left.x+right.x)/2.0;
mid.y=(left.y+right.y)/2.0;
mmid.x=(mid.x+right.x)/2.0;
mmid.y=(mid.y+right.y)/2.0;
t1=dist(A,mid)/p+Three2(mid);
t2=dist(A,mmid)/p+Three2(mmid);
if(t1>t2)
left=mid;
else
right=mmid;
}
return t1;
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
while(t--){
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);
printf("%.2lf\n",Three1());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息