代码
def gbs(x, y):
if x>y:
i = 1
while((x*i)<=x*y):
if (x*i)%y==0:
return x*i
else:
i += 1
elif x<y:
i = 1
while((y*i)<=y*x):
if (y*i)%x==0:
return y*i
else:
i += 1
else:
return x
maxx = 500000000
gra = [[0]*2021 for i in range(2021)]
for i in range(2021):
for j in range(2021):
if i==j:
weight = maxx
elif abs(i-j)>21:
weight = maxx
elif abs(i-j)<=21:
weight = gbs(i+1,j+1)
gra[i][j] = weight
dist = list(0 for i in range(2021))
path = list(0 for i in range(2021))
s = list(0 for i in range(2021))
for j in range(2021):
s[j] = 0
dist[j] = gra[0][j]
if gra[0][j]==maxx:
path[j] = -1
else:
path[j] = 0
s[0] = 1
path[0] = 0
dist[0] = 0
while(0 in s):
minn = maxx
for i in range(len(dist)):
if s[i]==0 and dist[i]<minn:
minn = dist[i]
j = i
s[j] = 1
for k in range(len(dist)):
if s[k]==0:
if dist[k]>dist[j]+ gra[j][k]:
dist[k] = dist[j]+ gra[j][k]
path[k] = j
print(dist[2020])
代码
- 建立二维数组
aList = [[0] * cols for i in range(rows)]
- 建立一维数组
s = list(0 for i in range(nums))