本题是最速降线问题的一种变体,需要找出以$1/y$作为cost函数时从$A$到$B$的最小cost。
此时的最优路径其解析形式是以$AB$为直径的半圆,本题只能在整点间移动,因此设置一个上界(此处使用的是0.5),找到离半圆弧距离不超过此上界的点,在这些点上作动态规划即可。
最终结果是$18.420738199$。
注:为便于作除法、使用math.isqrt函数、以及格式化输出,以下代码为Python 3。
import math
n,n2,n3=10000,5000,100
threshold=0.5
v=lambda y0,y1:1.0*y if y0==y1 else (y1-y0)/(math.log(y1)-math.log(y0))
d=lambda x0,y0,x1,y1: math.sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0))
P=[(i,j) for i in range(n2+1) for j in range(max(1,math.isqrt(n*i-i*i)-n3),min(n2+1,math.isqrt(n*i-i*i)+n3)) if abs(d(n2,0,i,j)-n2) < threshold]
res={}
for i,(x,y) in enumerate(P):
if x==0 or y==1:
res[x,y]=d(0,1,x,y)/v(1,y)
else:
res[x,y] = min(res[a,b]+d(a,b,x,y)/v(b,y) for (a,b) in P[max(0,i-n3):i])
print(f"{2*res[n2,n2]:.9f}")
|