HDU 3400 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. 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
由于三分的是点的坐标,所以刚拿到题的时候思路不清晰。先在cd上找一点使从ab到这点加上这点到d的时间最小finf2(),再在ab上找一点使a到这点的时间加上find2()的结果最小
#include<cstdio>
#include<cmath>
#define eps 1e-5
struct point
{
    int x,y;
};
int dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int p,q,r;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值