题目:
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? InputThe 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 OutputThe 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
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
1 0 0 0 100 100 0 100 100 2 2 1
题意:给你两条平面坐标中的线段AB,CD的坐标.有一人想从A点走到D点,已知这人在AB上的速度p,在CD上的速度q和在坐标系其他地方的速度r.求A到达D的最短时间.
思路:既然要求时间短,肯定是在AB上找一点E从A走到E然后在CD上找一点F又从E走到F,然后走FD.那么问题是如何确定这个点.根据总时间s=p/AE+r/EF+q/fD可得.于是我们分别对AB和CD进行三分来寻找满足要求的最小的点E和F.也就是嵌套三分.具体见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
struct Point
{
double x,y;
}a,b,c,d,e,f,g;
double p,q,r;
int T,n,m;
double get_dis(Point v,Point u)
{
return sqrt((v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y));
}
double cal(double rat_cd)//计算cd以及其余路上的长度
{
f.x=c.x+(d.x-c.x)*rat_cd;
f.y=c.y+(d.y-c.y)*rat_cd;
return get_dis(f,d)/q+get_dis(e,f)/r;
}
double ts_2(double rat_ab)//三分cd中f点的位置
{
e.x=a.x+(b.x-a.x)*rat_ab;
e.y=a.y+(b.y-a.y)*rat_ab;
double l=0,r=1.0;
while(r-l>eps)
{
double mid,midmid;
mid=(l+r)/2;
midmid=(mid+r)/2;
if(cal(mid)>cal(midmid))
l=mid;
else
r=midmid;
}
return get_dis(a,e)/p+cal(l);
}
double ts_1()//三分ab中的e点所在位置
{
double l=0,r=1.0;
while(r-l>eps)
{
double mid,midmid;
mid=(l+r)/2;
midmid=(mid+r)/2;
if(ts_2(mid)>ts_2(midmid))
l=mid;
else
r=midmid;
}
return ts_2(l);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
scanf("%lf%lf%lf",&p,&q,&r);
printf("%.2f\n",ts_1());
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
struct Point
{
double x,y;
}a,b,c,d,e,f,g;
double p,q,r;
int T,n,m;
double get_dis(Point v,Point u)
{
return sqrt((v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y));
}
double cal(double rat_cd)//计算cd以及其余路上的长度
{
f.x=c.x+(d.x-c.x)*rat_cd;
f.y=c.y+(d.y-c.y)*rat_cd;
return get_dis(f,d)/q+get_dis(e,f)/r;
}
double ts_2(double rat_ab)//三分cd中f点的位置
{
e.x=a.x+(b.x-a.x)*rat_ab;
e.y=a.y+(b.y-a.y)*rat_ab;
double l=0,r=1.0;
while(r-l>eps)
{
double mid,midmid;
mid=(l+r)/2;
midmid=(mid+r)/2;
if(cal(mid)>cal(midmid))
l=mid;
else
r=midmid;
}
return get_dis(a,e)/p+cal(l);
}
double ts_1()//三分ab中的e点所在位置
{
double l=0,r=1.0;
while(r-l>eps)
{
double mid,midmid;
mid=(l+r)/2;
midmid=(mid+r)/2;
if(ts_2(mid)>ts_2(midmid))
l=mid;
else
r=midmid;
}
return ts_2(l);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
scanf("%lf%lf%lf",&p,&q,&r);
printf("%.2f\n",ts_1());
}
return 0;
}