HDU 3400 Line belt (三分搜索)

Line belt

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



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
 

Source
 

Recommend
lcy

題意:

給你一個平面,平面上有兩條線段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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章