g_s=[1,0,0,3,2] g_e=[2,1,4,4,3] g_w=[2,-3,5,2,3] dis=[0,-1,-1,-1,-1] for t in range(5): if dis[t]==-1: dis[t]=float("inf") v=5 e=5 for k in range(v): change=False for i in range(e): if dis[g_e[i]]>dis[g_s[i]]+g_w[i]: dis[g_e[i]]=dis[g_s[i]]+g_w[i] change=True #优化 if not change: break print(dis)