bzoj1857
1857: [Scoi2010]传送带
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1487 Solved: 814
[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
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
从黄学长那里抄了个三分
三分是一个乱搞利器,因为即使一个函数不是单峰的,三分也可以找到一个较优的解,实在不行还有退火套退火。。。
这道题三分两次,先三分第一条传送带上走多远,在第一个基础上再三分第二个传送带上走多远
#include<bits/stdc++.h> using namespace std; const double eps = 1e-3; struct position { int x, y; } A, B, C, D; int p, q, r; inline double dis(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } inline double cal(double x, double y) { double lx = C.x, ly = C.y, rx = D.x, ry = D.y; double x1, x2, y1, y2, t1, t2; while(fabs(rx - lx) > eps || fabs(ry - ly) > eps) { x1 = lx + (rx - lx) / 3; y1 = ly + (ry - ly) / 3; x2 = lx + (rx - lx) / 3 * 2; y2 = ly + (ry - ly) / 3 * 2; t1 = dis(x, y, A.x, A.y) / p + dis(x, y, x1, y1) / r + dis(x1, y1, D.x, D.y) / q; t2 = dis(x, y, A.x, A.y) / p + dis(x, y, x2, y2) / r + dis(x2, y2, D.x, D.y) / q; if(t1 > t2) lx = x1, ly = y1; else rx = x2, ry = y2; } return dis(x, y, A.x, A.y) / p + dis(lx, ly, x, y) / r + dis(lx, ly, D.x, D.y) / q; } int main() { scanf("%d%d%d%d", &A.x, &A.y, &B.x, &B.y); scanf("%d%d%d%d", &C.x, &C.y, &D.x, &D.y); scanf("%d%d%d", &p, &q, &r); double lx = A.x, ly = A.y, rx = B.x, ry = B.y; double x1, x2, y1, y2, t1, t2; while(fabs(rx - lx) > eps || fabs(ry - ly) > eps) { x1 = lx + (rx - lx) / 3; y1 = ly + (ry - ly) / 3; x2 = lx + (rx - lx) / 3 * 2; y2 = ly + (ry - ly) / 3 * 2; t1 = cal(x1, y1); t2 = cal(x2, y2); if(t1 > t2) lx = x1, ly = y1; else rx = x2, ry = y2; } printf("%.2lf\n", cal(lx, ly)); return 0; }