Line belt
Total Submission(s): 2329 Accepted Submission(s): 880
How long must he take to travel from A to D?
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
題意:
給你一個平面,平面上有兩條線段AB和CD,某東西在AB上跑的速度是P,在CD上跑的速度是Q,在平面其他地方跑的速度是R,問從A點到D點的最快時間。思路:
嵌套的三分搜索
1.首先明確需要在AB和CD上分別確定一個點作爲轉折點。
2.若已知AB,CD上的定點P1,P2,那麼Time = AP1/P + P1P2/R + P2D/Q;
3.若我們能夠先知道點P1(x1,y1),那麼可以根據P2(x2,y2)求出最快的時間Time。
Time = AP1/P + sqrt((x1-x2)^2 + (y1-y2)^2)/R + sqrt((x2-D.x) + (y2-D.y))/Q;
/*************************************************************************
> File Name: hdu3400.cpp
> Author: BSlin
> Mail:
> Created Time: 2013年09月29日 星期日 22時07分59秒
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-5
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif
struct Point {
double x,y;
}A,B,C,D;
int P,Q,R;
Point Get_Point (Point A, Point B) {
Point Mid;
Mid.x = (A.x + B.x) / 2.0;
Mid.y = (A.y + B.y) / 2.0;
return Mid;
}
double Get_Len (Point A, Point B) {
return sqrt((A.x -B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
double Get_Time (Point p) {
Point lift = C,right = D,mid,mmid;
double mid_time = 0,mmid_time = 1;
while(fabs(mid_time - mmid_time) > esp) {
mid = Get_Point(lift,right);
mmid = Get_Point(mid,right);
mid_time = Get_Len(p,mid) / R + Get_Len(mid,D) / Q;
mmid_time = Get_Len(p,mmid) / R + Get_Len(mmid,D) / Q;
if(mid_time - mmid_time >= esp) lift = mid;
else right = mmid;
}
return mid_time + Get_Len(A,p) / P;
}
int main(int argc, char** argv) {
//read;
int t;
scanf("%d",&t);
while(t--) {
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%d%d%d",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&D.x,&D.y,&P,&Q,&R);
Point lift = A,right = B,mid,mmid;
double mid_time = 0,mmid_time = 1;
while(fabs(mid_time - mmid_time) > esp) {
mid = Get_Point(lift,right);
mmid = Get_Point(mid,right);
mid_time = Get_Time(mid);
mmid_time = Get_Time(mmid);
if(mid_time - mmid_time >= esp) lift = mid;
else right = mmid;
}
printf("%.2f\n", mid_time);
}
return 0;
}