ACM--steps--4.1.6--Line belt

Line belt

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 426 Accepted Submission(s): 228
 
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
HDOJ Monthly Contest – 2010.05.01
 
Recommend
lcy


#include
#include
#include
using namespace std;
//我反正是没看出来为啥是凹性函数,不过要求一个最小时间,肯定会有一个最低点,,,
double p,q,r;//表示三个速度。
struct point
{
    double x,y;//坐标点;
};
double dis(point a,point b)
{
    //计算两点之间的距离
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dyx(point a,point c,point d)
{
    double t1,t2;//表示两个时间。
    //t1=t2=0;
    point left,right,mid,mmid;
    left=c;
    right=d;
    do
    {
        //对CD线段进行三分。
        mid.x=(left.x+right.x)/2;
        mid.y=(left.y+right.y)/2;
        mmid.x=(mid.x+right.x)/2;
        mmid.y=(mid.y+right.y)/2;
        t1=dis(a,mid)/r+dis(mid,d)/q;
        t2=dis(a,mmid)/r+dis(mmid,d)/q;
        if(t1>t2)
        left=mid;
        else
        right=mmid;
    }while(fabs(t1-t2)>0.000001);
    return t1;
}
double wyx(point a,point b,point c,point d)
{
    //先三分第一条线段,找出一个点。
    double t1,t2;//表示两个时间。
    t1=t2=0;
    point left,right,mid,mmid;
    left=a;
    right=b;
    do
    {
        //三分法先取两点之间的中点,再取中点与右端点的中点。
        mid.x=(left.x+right.x)/2;
        mid.y=(left.y+right.y)/2;
        mmid.x=(mid.x+right.x)/2;
        mmid.y=(mid.y+right.y)/2;
        t1=dis(mid,a)/p+dyx(mid,c,d);
        t2=dis(mmid,a)/p+dyx(mmid,c,d);
        if(t1>t2)
        left=mid;
        else
        right=mmid;
    }while(fabs(t1-t2)>0.000001);
    return t1;
}
int main()
{
    int T;
    cin>>T;
    point a,b,c,d;
    while(T--)
    {
        cin>>a.x>>a.y>>b.x>>b.y;
        cin>>c.x>>c.y>>d.x>>d.y;
        cin>>p>>q>>r;
        cout<


你可能感兴趣的:(三分查找,基础)