从零开始写代码 Python 8分钟实现Floyd最短路径算法
作者:互联网
B站视频链接:从零开始写代码 Python 8分钟实现Floyd最短路径算法_哔哩哔哩_bilibili
ppt
代码为
network=[[0,1,0,2,0,0], [1,0,2,4,3,0], [0,2,0,0,1,4], [2,4,0,0,6,0], [0,3,1,6,0,2], [0,0,4,0,2,0]] def Floyd(network): FMAX = 999 n = len(network) d = [[network[i][j] if network[i][j] !=0 \ else FMAX for i in range(n)]for j in range(n)] for k in range(n):# 中转节点 for i in range(n):# 起始节点 for j in range(n):# 结束节点 # 起始节点→结束节点 # 起始节点→中转节点→结束节点 # 之间的最小值 d[i][j] = min(d[i][j], d[i][k]+d[k][j]) return d res = Floyd(network) for item in res: print(item)
标签:network,Python,Floyd,起始,range,从零开始,哔哩,节点 来源: https://www.cnblogs.com/ljy1227476113/p/16364896.html