NC50239. 传送带
描述
输入描述
输入数据第一行是4个整数,表示A和B的坐标,分别为;
第二行是4个整数,表示C和D的坐标,分别为,;
第三行是3个整数,分别是P,Q,R。
输出描述
输出数据为一行,表示lxhgww从A点走到D点的最短时间,保留到小数点后2位。
示例1
输入:
0 0 0 100 100 0 100 100 2 2 1
输出:
136.60
C++14(g++5.4) 解法, 执行用时: 6ms, 内存消耗: 504K, 提交时间: 2020-02-16 22:15:42
#include<bits/stdc++.h> using namespace std; double ax,ay,bx,by,cx,cy,dx,dy,p,q,r; double dist(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } double check(double x,double y){ double xl=cx,xr=dx,yl=cy,yr=dy; while(fabs(xr-xl)>1e-6||fabs(yr-yl)>1e-6){ double x1,x2,y1,y2; x1=xl+(xr-xl)/3.0; x2=xr-(xr-xl)/3.0; y1=yl+(yr-yl)/3.0; y2=yr-(yr-yl)/3.0; double t1=dist(1.0*ax,1.0*ay,x,y)/p+dist(x,y,x1,y1)/r+dist(x1,y1,1.0*dx,1.0*dy)/q; double t2=dist(1.0*ax,1.0*ay,x,y)/p+dist(x,y,x2,y2)/r+dist(x2,y2,1.0*dx,1.0*dy)/q; if(t1<=t2){xr=x2;yr=y2;} else xl=x1,yl=y1; } return dist(1.0*ax,1.0*ay,x,y)/p+dist(x,y,xl,yl)/r+dist(xl,yl,1.0*dx,1.0*dy)/q; } int main(){ cin>>ax>>ay>>bx>>by; cin>>cx>>cy>>dx>>dy; cin>>p>>q>>r; double xl,xr,yl,yr; xl=ax,xr=bx,yl=ay,yr=by; while(fabs(xr-xl)>1e-6||fabs(yr-yl)>1e-6){ double x1,x2,y1,y2; x1=xl+(xr-xl)/3.0; x2=xr-(xr-xl)/3.0; y1=yl+(yr-yl)/3.0; y2=yr-(yr-yl)/3.0; if(check(x1,y1)<check(x2,y2)) xr=x2,yr=y2; else xl=x1,yl=y1; } printf("%.2lf\n",check(xl,yl)); return 0; }
C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 408K, 提交时间: 2023-03-06 18:07:00
#include<bits/stdc++.h> using namespace std; double Ax,Ay,Bx,By,Cx,Cy,Dx,Dy,p,q,r,ans; double check(double x,double y) { double P1x=Ax+(Bx-Ax)*x,P1y=Ay+(By-Ay)*x,P2x=Cx+(Dx-Cx)*y,P2y=Cy+(Dy-Cy)*y; return sqrt((P1x-Ax)*(P1x-Ax)+(P1y-Ay)*(P1y-Ay))/p+sqrt((Dx-P2x)*(Dx-P2x)+(Dy-P2y)*(Dy-P2y))/q+sqrt((P2x-P1x)*(P2x-P1x)+(P2y-P1y)*(P2y-P1y))/r; } double find2(double x,double l,double r) { double res=0.0; while(r-l>=1e-6) { double midl=l+(r-l)/3; double midr=r-(r-l)/3; double res1=check(x,midl); double res2=check(x,midr); if(res1<res2) { r=midr; res=res1; } else l=midl,res=res2; } return res; } void find1(double l,double r) { while(r-l>=1e-6) { double midl=l+(r-l)/3; double midr=r-(r-l)/3; double res1=find2(midl,0,1); double res2=find2(midr,0,1); if(res1<res2) { r=midr; ans=res1; } else { l=midl; ans=res2; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>Ax>>Ay>>Bx>>By>>Cx>>Cy>>Dx>>Dy>>p>>q>>r; find1(0,1); printf("%.2f",ans); }
C++(clang++ 11.0.1) 解法, 执行用时: 513ms, 内存消耗: 700K, 提交时间: 2023-07-18 20:00:24
#include<bits/stdc++.h> using namespace std; #define int long long double getDis(double x1,double y1,double x2,double y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } void __main__() { double ax,ay,bx,by,cx,cy,dx,dy;cin>>ax>>ay>>bx>>by>>cx>>cy>>dx>>dy; double p,q,r;cin>>p>>q>>r; int tim=5000; double res=getDis(ax,ay,dx,dy)/r,ix=ax,iy=ay; for(int i=0;i<=tim;++i) { ix=ax+(bx-ax)*i/tim,iy=ay+(by-ay)*i/tim; double jx=cx,jy=cy; for(int j=0;j<=tim;++j) jx=cx+(dx-cx)*j/tim,jy=cy+(dy-cy)*j/tim, res=min(res,getDis(ax,ay,ix,iy)/p+getDis(ix,iy,jx,jy)/r+getDis(jx,jy,dx,dy)/q); } cout<<res<<endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cout<<fixed<<setprecision(2); __main__(); system("pause"); return 0; }