HDU 3400 Line belt (三分法)

Line belt

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


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
 

 

Recommend
lcy
 
 
 
神奇的三分法。
就是利用凸性。
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const double eps=1e-5;
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 p,q,r;

double find2(point a,point c,point d)
{
    point left,right;
    point mid,midmid;
    double t1,t2;
    left=c;right=d;
    do
    {
        mid.x=(left.x+right.x)/2;
        mid.y=(left.y+right.y)/2;
        midmid.x=(mid.x+right.x)/2;
        midmid.y=(mid.y+right.y)/2;
        t1=dis(a,mid)/r+dis(mid,d)/q;
        t2=dis(a,midmid)/r+dis(midmid,d)/q;
        if(t1>t2)left=mid;
        else right=midmid;
    }
    while(dis(left,right)>=eps);
    return t1;
}
double find(point a,point b,point c,point d)
{
    point left,right;
    point mid,midmid;
    double t1,t2;
    left=a;
    right=b;
    do
    {
        mid.x=(left.x+right.x)/2;
        mid.y=(left.y+right.y)/2;
        midmid.x=(mid.x+right.x)/2;
        midmid.y=(mid.y+right.y)/2;
        t1=dis(a,mid)/p+find2(mid,c,d);
        t2=dis(a,midmid)/p+find2(midmid,c,d);
        if(t1>t2)left=mid;
        else right=midmid;
    }while(dis(right,left)>=eps);
    return t1;
}
int main()
{
    int T;
    point a,b,c,d;
    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);
        printf("%.2lf\n",find(a,b,c,d));
    }
    return 0;
}

 

posted on 2012-09-02 09:41  kuangbin  阅读(1476)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: