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;
}