Line belt HDU

来源:互联网 发布:js object 数组去重 编辑:程序博客网 时间:2024/06/17 13:24

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

平面上两条直线AB,CD,动点从A出发,在某点离开AB前往CD某点,再到达D点,各直线和平面上移动速度不同,求最少时间。
从AB哪点出发,和到达CD哪点都是需要查找的对象,所以要两次三分,先三分查找第一个点,再在该情况下找最优的第二个点。

#include <stdio.h>#include <math.h>int T,i,j;double s,p,q,r;struct node{    double x,y;    node(int xx=0,int yy=0):x(xx),y(yy){}}a,b,c,d;double dis(node aa,node bb){    double re=sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));    return re;}double sf2(node n){    double t1,t2;    node low(c.x,c.y);    node high(d.x,d.y);    node m1,m2;    do{        m1.x=(low.x+high.x)/2;        m1.y=(low.y+high.y)/2;        m2.x=(low.x+m1.x)/2;        m2.y=(low.y+m1.y)/2;        t1=dis(m1,d)/q+dis(n,m1)/r;        t2=dis(m2,d)/q+dis(n,m2)/r;        if(t1<t2){            low.x=m2.x;            low.y=m2.y;        }else{            high.x=m1.x;            high.y=m1.y;        }    }while(dis(low,high)>1e-8);    return t1;}double sf1(){    double t1,t2;    node low(a.x,a.y);    node high(b.x,b.y);    node m1,m2;    do{        m1.x=(low.x+high.x)/2;        m1.y=(low.y+high.y)/2;        m2.x=(low.x+m1.x)/2;        m2.y=(low.y+m1.y)/2;        t1=sf2(m1)+dis(m1,a)/p;        t2=sf2(m2)+dis(m2,a)/p;        if(t1<t2){            low.x=m2.x;            low.y=m2.y;        }else{            high.x=m1.x;            high.y=m1.y;        }    }while(dis(low,high)>1e-8);    return t1;}int main(){    scanf("%d",&T);    while(T--){        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);        scanf("%lf%lf%lf",&p,&q,&r);        s=sf1();        printf("%.2lf\n",s);    }    return 0;}
原创粉丝点击