练习二1006

Line belt

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :32768/32768K (Java/Other)

Total Submission(s) : 40   Accepted Submission(s) : 10

Problem Description

In atwo-dimensional plane there are two line belts, there are two segments AB andCD, lxhgww's speed on AB is P and on CD is Q, he can move with the speed R onother area on the plane.<br>How long must he take to travel from A to D?

 

 

Input

The first line isthe case number T.<br>For each case, there are three lines.<br>Thefirst line, four integers, the coordinates of A and B: Ax Ay BxBy.<br>The second line , four integers, the coordinates of C and D:Cx CyDx 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 timeto travel from A to D, round to two decimals.

 

Sample Input

1<br>0 0 0100<br>100 0 100 100<br>2 2 1


Sample Output

136.60


题意:

就是给你两条线段AB , CD ,一个人在AB以速度p跑,在CD上以q跑,在其他地方跑速度是r。问你从A到D最少的时间。


解题思路:

先三分AB上的点,再三分CD上的点即可。

首先,时间最短的路径必定是至多3条直线段构成的,一条在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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值