2016SDAU編程練習二1006

Line belt 


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.<br>How long must he take to travel from A to D?
 


Input
The first line is the case number T.<br>For each case, there are three lines.<br>The first line, four integers, the coordinates of A and B: Ax Ay Bx By.<br>The second line , four integers, the coordinates of C and D:Cx Cy Dx Dy.<br>The third line, three integers, P Q R.<br>0<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000<br>1<=P,Q,R<=10
 


Output
The minimum time to travel from A to D, round to two decimals.
 


Sample Input
1<br>0 0 0 100<br>100 0 100 100<br>2 2 1 


Sample Output
136.60 


Author
lxhgww&&momodi
 


Source
HDOJ Monthly Contest – 2010.05.01

 

題意:給出兩條傳送帶的起點到末端的座標,其中ab爲p的速度,cd爲q的速度 其他地方爲r的速度,求a到d點的最短時間

思路:三分法,就是給了兩條線段,每條中必定有一點,連接後時間最短

感想:這個就比較複雜一點了

AC代碼

#include <cstdio>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#include<iomanip>
using namespace std;
double p,q,R;
struct point
{
    double x;
    double y;
    point operator / (const point &others)
    {
        point tt;
        tt.x=(this->x+others.x)/2;
        tt.y=(this->y+others.y)/2;
        return tt;
    }
};
double length (point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double find ( point c,point d,point mid )
{
    point l,r,mid1,mid2;
    double t1,t2;
    l=c;r=d;
    do
    {
        mid1=(r/l);
        mid2=(r/mid1);
        t1=length(mid1,d)/q+length(mid1,mid)/R;
        t2=length(mid2,d)/q+length(mid2,mid)/R;
        if(t1>t2)
            l=mid1;
        else
            r=mid2;
    }while(fabs(t1-t2)>0.00001);
    return t2;
}
int main()
{
   // freopen("r.txt","r",stdin);
    int N;
    point a,b,c,d,r,l,mid1,mid2;
    double t1,t2;
    cin>>N;
    while(N--)
    {
        cin>>a.x>>a.y>>b.x>>b.y;
        cin>>c.x>>c.y>>d.x>>d.y;
        cin>>p>>q>>R;
        l=a;r=b;
        do
        {
            mid1=(r/l);
            mid2=(r/mid1);
            t1=length(a,mid1)/p+find(c,d,mid1);
            t2=length(a,mid2)/p+find(c,d,mid2);
            if(t1>t2)
                l=mid1;
            else
                r=mid2;
        }while(fabs(t2-t1)>0.00001);
        printf("%.2f\n",t2);


    }
    return 0;




}

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