BZOJ1857: [Scoi2010]传送带(三分套三分)

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 2005  Solved: 1091
[Submit][Status][Discuss]

Description

在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段。两条传送带分别为线段AB和线段CD。lxhgww在AB上的移动速度为P,在CD上的移动速度为Q,在平面上的移动速度R。现在lxhgww想从A点走到D点,他想知道最少需要走多长时间

Input

输入数据第一行是4个整数,表示A和B的坐标,分别为Ax,Ay,Bx,By 第二行是4个整数,表示C和D的坐标,分别为Cx,Cy,Dx,Dy 第三行是3个整数,分别是P,Q,R

Output

输出数据为一行,表示lxhgww从A点走到D点的最短时间,保留到小数点后2位

Sample Input

0 0 0 100
100 0 100 100
2 2 1


Sample Output

136.60

HINT

对于100%的数据,1<= Ax,Ay,Bx,By,Cx,Cy,Dx,Dy<=1000
1<=P,Q,R<=10

Source

 
很显然最优的路线一定是从A走到AB上的一点走到CD上的一点再走到D
然后三分套三分就可以了
 
 
#include<cstdio>
#include<cmath>
#include<algorithm>
#define eps 1e-3
using namespace std;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
    return x * f;
}
int Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, P, Q, R;
double dis(double x, double y, double x2, double y2) {
    return sqrt((x2 - x) * (x2 -x) + (y2 - y) * (y2 - y));
}
double check2(double x,double y,double x2,double y2) {
    return dis(Ax, Ay, x, y) / P + dis(x, y, x2, y2) / R + dis(x2, y2, Dx, Dy) / Q;
}
double check(double x, double y) {
    double lx = Cx, ly = Cy, rx = Dx, ry = Dy, a, b;
    while(abs(rx - lx) > eps || abs(ry - ly) > eps) {
        double wx1 = (lx * 2 + rx) / 3, wy1 = (ly * 2 + ry) / 3,
               wx2 = (lx + rx * 2) / 3, wy2 = (ly + ry * 2) / 3;
        a = check2(x, y, wx1, wy1);
        b = check2(x, y, wx2, wy2);
        if(a > b) lx = wx1, ly = wy1;
        else rx = wx2, ry = wy2;
    }
    return check2(x, y, lx, ly); 
}
int main() {
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #endif
    Ax = read(), Ay = read(), Bx = read(), By = read(), Cx = read(), Cy = read(), Dx = read(), Dy = read(), P = read(), Q = read(), R = read();
    double lx = Ax, ly = Ay, rx = Bx, ry = By;
    while(abs(rx - lx) > eps || abs(ry - ly) > eps) {
        double wx1 = (lx * 2 + rx) / 3, wy1 = (ly * 2 + ry) / 3,
               wx2 = (lx + rx * 2) / 3, wy2 = (ly + ry * 2) / 3;
        if(check(wx1, wy1) > check(wx2, wy2)) lx = wx1, ly = wy1;
        else rx = wx2, ry = wy2;
    }
    printf("%.2lf", check(lx, ly));
    return 0;
}

 

 
posted @ 2018-05-03 21:00  自为风月马前卒  阅读(373)  评论(0编辑  收藏  举报

Contact with me