graph = [[0,1,3,6],
[1,0,1,5],
[3,1,0,2],
[6,5,2,0]]
inf = 100000
n = 4
d = [0,inf,inf,inf]
p = [-1,-1,-1,-1]
def relax(a,b):
if(d[b] > d[a] + graph[a][b]):
d[b] = d[a] + graph[a][b]
p[b] = a+1
for k in range(n-1):
for i in range(n):
for j in range(n):
relax(i,j)
print d
print p
主要就是relax()的问题。多次循环,不停地relax()就好了。