hdu 3400 Line belt

来源:互联网 发布:程序员怎么找兼职 编辑:程序博客网 时间:2024/05/19 00:47

Line belt

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


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
10 0 0 100100 0 100 1002 2 1
 

Sample Output
136.60
 

Author
lxhgww&&momodi
 

题意:
        已知两条线段,分别知道在两条线段上AB,CD的速度p,q和不在两条线段上的速度r,求从A点到D点的最小时间。

题解:
         三分题,时间最短的路径必定是至多3条直线段构成的,一条在AB上,一条在CD上,一条在两条线段之间通过三分AB间的位置,确定在AB的位置,后三分CD的位置找到最小值,这样两个三分嵌套在一起就可以了。
        至于为什么用三分,可以想下直接从A到D距离最短,但是有可能因为速度比较慢而耽误时间,可以在AB,CD上走一段来缩小时间,但是在AB,CD上走得过于多就会因为路程增加而导致时间增加,所以是单峰函数。

代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const double eps=1e-6;struct node{    double x;    double y;}a,b,c,d,bb,dd;double ab,cd;double p,q,r;double dis(node u,node v){    return  sqrt(eps+(u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));}double work(double t){    dd.x=d.x+(c.x-d.x)/cd*t*q;    dd.y=d.y+(c.y-d.y)/cd*t*q;    return t+dis(bb,dd)/r;}double solve(double t){     bb.x=a.x+(b.x-a.x)/ab*t*p;     bb.y=a.y+(b.y-a.y)/ab*t*p;    double l=0,h=cd/q,mid1,mid2;    while(l+eps<h)    {        mid1=l+(h-l)/3;        mid2=h-(h-l)/3;        if(work(mid1)<work(mid2))        {            h=mid2;        }        else        l=mid1;    }    return t+work(l);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);        scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y);        scanf("%lf%lf%lf",&p,&q,&r);        ab=dis(a,b);        cd=dis(c,d);        double l=0.0,h=ab/p,mid1,mid2;        while(l+eps<h)        {            mid1=l+(h-l)/3;            mid2=h-(h-l)/3;            if(solve(mid1)<solve(mid2))            h=mid2;            else            l=mid1;        }        printf("%.2f\n",solve(mid1));    }    return 0;}


0 0
原创粉丝点击