三分_2

来源:互联网 发布:spss数据拟合曲线 编辑:程序博客网 时间:2024/05/16 13:38

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

10 0 0 100100 0 100 1002 2 1

Sample Output

136.60

题意是有两条线AB和CD,AB上速度是p,CD上速度是q,其他区域是R,问从A到D的最短时间。这样想就好理解了:先无视AB,问从平面上的一点x到D的最短时间,这显然有一个峰值,所以用三分;同理反过来对AB来说上也有这样一个点,所以只要把两个三分嵌套就解决问题了


#include<iostream>#include"math.h"#include"cstdio"const double EPS=1e-7;using namespace std;double  p,q,v;struct point{    double x;    double y;};double dist(point m,point n){    return  pow(pow(m.x-n.x,2)+pow(m.y-n.y,2),1.0/2);}double binary_CD(point c,point d,point rand){    double t1,t2;    point mid,mmid;    point L=c,R=d;    do{        mid.x=(R.x-L.x)/2+L.x;   mid.y=(R.y-L.y)/2+L.y;        mmid.x=(R.x-mid.x)/2+mid.x;   mmid.y=(R.y-mid.y)/2+mid.y;        t1=dist(d,mid)/p+dist(mid,rand)/v;        t2=dist(d,mmid)/p+dist(mmid,rand)/v;        if(t1>t2)  L=mid;        else  R=mmid;    }while(fabs(t1-t2)>EPS);    return t1;}double  binary(point a,point b,point c,point d){    double t1,t2;    point mid,mmid;    point L=a,R=b;    do{        mid.x=(R.x-L.x)/2+L.x;  mid.y=(R.y-L.y)/2+L.y;        mmid.x=(R.x-mid.x)/2+mid.x; mmid.y=(R.y-mid.y)/2+mid.y;        t1=dist(a,mid)/p+binary_CD(c,d,mid);        t2=dist(a,mmid)/p+binary_CD(c,d,mmid);        if(t1>t2)  L=mid;        else  R=mmid;    }while(fabs(t1-t2)>EPS);    return t1;}int main(){    int T;    point a,b,c,d;    cin>>T;    while(T--)    {        cin>>a.x>>a.y>>b.x>>b.y;        cin>>c.x>>c.y>>d.x>>d.y;        cin>>p>>q>>v;        printf("%.2lf\n",binary(a,b,c,d));    }    return 0;}


0 0
原创粉丝点击