标签:
Mean:
给出两条平行的线段AB, CD,然后一个人在线段AB的A点出发,走向D点,其中,人在线段AB上的速度为P, 在线段CD上的速度为Q,在其他地方的速度为R,求人从A点到D点的最短时间。
analyse:
经典的三分套三分。
首先在AB线段上三分,确定一个点,然后再在CD上三分,确定第二个点,计算出answer。也就是嵌套的三分搜索。
Time complexity: O(logn*logm)
Source code:
// Memory Time // 1347K 0MS // by : crazyacking // 2015-03-31-23.59 #include<map> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<cstdlib> #include<cstring> #include<climits> #include<iostream> #include<algorithm> #define MAXN 1000010 #define LL long long using namespace std; struct point { double x,y; }; point A,B,C,D; int p,q,r; double length(point X,point Y) { return sqrt((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y)); } double time(double a,double b) { point X,Y; X.x=a*(B.x-A.x)+A.x; X.y=a*(B.y-A.y)+A.y; Y.x=b*(C.x-D.x)+D.x; Y.y=b*(C.y-D.y)+D.y; return length(A,X)/p+length(D,Y)/q+length(X,Y)/r; } double ThiDiv(double lena) { double left=0.0,right=1.0,lm,rm; while(right-left>1e-6) { lm=(left*2.0+right)/3.0; rm=(left+right*2.0)/3.0; if(time(lena,lm)<=time(lena,rm)) right=rm; else left=lm; } return time(lena,left); } int main() { 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("%d %d %d",&p,&q,&r); double left=0.0,right=1.0,lm,rm; while(right-left>1e-6)//这个大于号居然写出了小于号,测试例子居然也过了,WA了N次 { //这里三分的是在AB上走的距离占AB比例 lm=(left*2+right)/3.0; rm=(left+2*right)/3.0; if(ThiDiv(lm)<=ThiDiv(rm)) right=rm; else left=lm; } printf("%.2f\n",ThiDiv(left)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/crazyacking/p/4382530.html