BZOJ1857 - [Scoi2010]传送带

2017/10/03

上午模拟赛做到了一个三分的题,然后想了假算法。

题目

给两个线段 (Ax, Ay) -> (Bx, By)(Cx, Cy) -> (Dx, Dy),在线段上走的速度分别是 P, Q,在其他地方走的速度是 R 。

A -> D 最短时间。

三分在两个线段上走的时间,有个玄学的地方(我没考虑到)就是直线可能退化成点,狂 WA 不止。

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-5;

double Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, P, Q, R;
double Dis1, Dis2;

inline double sqr(double x) { return x * x; }

double dist(double x1, double y1, double x2, double y2) {
    double ret = sqrt(sqr(x1 - x2) + sqr(y1 - y2));
//    if(std::isnan(ret)) while(true);
    return ret;
}

double G(double T1, double T2) {
    double R1 = Dis1 / P;
    double Ex = Ax + (Bx - Ax) * T1 / R1;
    double Ey = Ay + (By - Ay) * T1 / R1;
    
    double R2 = Dis2 / Q;
    double Fx = Dx + (Cx - Dx) * T2 / R2;
    double Fy = Dy + (Cy - Dy) * T2 / R2;
    
    return T1 + T2 + dist(Ex, Ey, Fx, Fy) / R;
}

double F(double T1) {
    double Le = 0, Ri = Dis2 / Q, Mid, Mid1, Mid2;
    
    while(Ri - Le > eps) {
        Mid = (Le + Ri) * 0.5;
        Mid1 = (Le + Mid) * 0.5;
        Mid2 = (Mid + Ri) * 0.5;
        
        if(G(T1, Mid1) < G(T1, Mid2)) Ri = Mid2;
        else Le = Mid1;
    }
    
    return G(T1, (Le + Ri) * 0.5);
}

int main() {
    scanf(
        "%lf %lf %lf %lf"
        "%lf %lf %lf %lf"
        "%lf %lf %lf"
    , &Ax, &Ay, &Bx, &By, &Cx, &Cy, &Dx, &Dy, &P, &Q, &R);
    
    Dis1 = dist(Ax, Ay, Bx, By);
    Dis2 = dist(Cx, Cy, Dx, Dy);
    
    if(Dis1 == 0.0) Dis1 += 1e-6;
    if(Dis2 == 0.0) Dis2 += 1e-6;
    
    double Le = 0, Ri = Dis1 / P, Mid, Mid1, Mid2;
    
    while(Ri - Le > eps) {
        Mid = (Le + Ri) * 0.5;
        Mid1 = (Le + Mid) * 0.5;
        Mid2 = (Mid + Ri) * 0.5;
        
        if(F(Mid1) < F(Mid2)) Ri = Mid2;
        else Le = Mid1;
    }
    
    printf("%.2lf", F((Ri + Le) * 0.5));
}