奥赛一本通 1439 传送带
1439 传送带
题目大意
平面上有两条传送带 $AB$ 与 $CD$,给出在两条传送带与平面上其他位置的移速,计算 $A$ 到 $D$ 的最短时间。
知识要点
直线、三分
解题思路
先三分枚举 $AB$ 上的 $E$,再三分枚举 $CD$ 上的 $F$,枚举的值为线段长度的占比。
参考代码
#include <bits/stdc++.h>
using namespace std;
struct Point {
double x, y;
} A, B, C, D, E, F;
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 calc(double s, double t) {
E.x = A.x + (B.x - A.x) * s;
E.y = A.y + (B.y - A.y) * s;
F.x = C.x + (D.x - C.x) * t;
F.y = C.y + (D.y - C.y) * t;
return dis(A, E) / P + dis(E, F) / R + dis(F, D) / Q;
}
double func(double s) {
double l = 0, r = 1, m1, m2;
while(r - l > 1e-6) {
m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
if(calc(s, m1) < calc(s, m2)) r = m2;
else l = m1;
}
return calc(s, l);
}
int main() {
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);
double l = 0, r = 1, m1, m2;
while(r - l > 1e-6) {
m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
if(func(m1) < func(m2)) r = m2;
else l = m1;
}
printf("%.2lf\n", func(l));
return 0;
}

浙公网安备 33010602011771号