HDU-3400Line belt-三分再三分-求距离中要加esp
传送门:Line belt
参考:http://blog.csdn.net/hcbbt/article/details/39375763
题意:
在一个平面途中,有一条路ab,还有一条路cd;假设在ab,cd和其他地方的运动速度不同;
求从a到d的最短时间;
思路:
三分在ab上的点,在三分cd上的点,找到对应最小的运动时间;
#include <iostream> #include <cstring> #include <algorithm> #include <string> #include <cstring> #include <cmath> using namespace std; const double esp = 1e-6; struct node { double x, y; }; node a, b, c, d, b2, d2; double ab, cd; double p, q, r; double dis(node a, node b) { return sqrt(esp + (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); //就是这里要加入esp } double get(double t) { d2.x = d.x + (c.x - d.x) / cd * t * q; d2.y = d.y + (c.y - d.y) / cd * t * q; return t + dis(b2, d2) / r; } double solve(double s) { b2.x = a.x + (b.x - a.x) / ab * s * p; b2.y = a.y + (b.y - a.y) / ab * s * p; double le = 0.0, ri = cd / q; while (le + esp < ri) { double m1 = le + (ri - le) / 3; double m2 = ri - (ri - le) / 3; if (get(m1) < get(m2)) { ri = m2; } else le = m1; } return s + get(le); } int main() { //freopen("in","r",stdin); 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 le = 0.0, ri = ab / p; while (le + esp < ri) { double m1 = le + (ri - le) / 3; double m2 = ri - (ri - le) / 3; if (solve(m1) < solve(m2)) { ri = m2; } else le = m1; } printf("%.2lf\n", solve(le)); } return 0; }
skr