题目链接
1 0 0 0 100 100 0 100 100 2 2 1
136.60
题意:两条线段AB和CD。起点在A,终点在D。在线段AB内的速度为p,在线段CD内的速度为q,在其它区域的速度为R。求从起点到终点的最短时间。
题解:先三分在AB线段的终点,在三分CD线段的起点。
代码如下:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<string> #include<stack> #include<math.h> #include<string.h> #include<vector> #include<set> #include<map> #define nn 110000 #define inff 0x7fffffff #define eps 1e-8 #define mod 1000000007 typedef long long LL; const LL inf64=LL(inff)*inff; using namespace std; struct point { double x,y; point(double x=0,double y=0):x(x),y(y){} }; point operator + (point A,point B) { return point(A.x+B.x,A.y+B.y); } point operator/(point A,double B) { return point(A.x/B,A.y/B); } point operator - (point a,point b) { return point(a.x-b.x,a.y-b.y); } double dis(point A,point B) { return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); } point A,B,C,D; double p,q,r; double check(point X,point Y) { return dis(A,X)/p+dis(X,Y)/r+dis(Y,D)/q; } double solve(point X) { point l=C,r=D; point mid,mmid; double ix,fc; while(dis(l,r)>eps) { mid=l+(r-l)/3.0; mmid=r-(r-l)/3.0; ix=check(X,mid); fc=check(X,mmid); if(ix>fc) { l=mid; } else r=mmid; } return check(X,l); } 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("%lf%lf%lf",&p,&q,&r); point l=A,r=B; point mid,mmid; double ix,fc; while(dis(l,r)>eps) { mid=l+(r-l)/3.0; mmid=r-(r-l)/3.0; ix=solve(mid),fc=solve(mmid); if(ix>fc) { l=mid; } else r=mmid; } printf("%.2lf\n",solve(l)); } return 0; }