题目大意:给出平面上两条线段,在这两条线段上走有一定的速度,在其他的平面上走也有一定的速度,问从A点到D点最少需要多少时间。
思路:好像是三分吧,大概感受一下吧,反正也不会证。
CODE:
#include <cmath> #include <cstdio> #include <iomanip> #include <cstring> #include <iostream> #include <algorithm> #define EPS 1e-7 #define INF 1e15 using namespace std; struct Point{ double x,y; Point(double _,double __):x(_),y(__) {} Point() {} void Read() { scanf("%lf%lf",&x,&y); } Point operator +(const Point &a)const { return Point(x + a.x,y + a.y); } Point operator -(const Point &a)const { return Point(x - a.x,y - a.y); } Point operator *(double a)const { return Point(x * a,y * a); } Point operator /(double a)const { return Point(x / a,y / a); } }A,B,C,D; inline double Calc(const Point &a,const Point &b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double v1,v2,v3; double TriDivide(const Point &p,Point l,Point r) { while(Calc(l,r) > EPS) { Point u = (r - l) / 3.0; Point p1 = l + u,p2 = p1 + u; double ans1 = Calc(A,p) / v1 + Calc(p1,D) / v2 + Calc(p,p1) / v3; double ans2 = Calc(A,p) / v1 + Calc(p2,D) / v2 + Calc(p,p2) / v3; if(ans1 > ans2) l = p1; else r = p2; } return Calc(A,p) / v1 + Calc(l,D) / v2 + Calc(p,l) / v3; } double TriDivide(Point l,Point r) { while(Calc(l,r) > EPS) { Point u = (r - l) / 3.0; Point p1 = l + u,p2 = p1 + u; double ans1 = TriDivide(p1,C,D),ans2 = TriDivide(p2,C,D); if(ans1 > ans2) l = p1; else r = p2; } return TriDivide(l,C,D); } int main() { A.Read(),B.Read(),C.Read(),D.Read(); scanf("%lf%lf%lf",&v1,&v2,&v3); cout << fixed << setprecision(2) << TriDivide(A,B) << endl; return 0; }
原文地址:http://blog.csdn.net/jiangyuze831/article/details/42738125