习题6.4
1.代码实现
点击查看代码
import numpy as np
import networkx as nx
a=np.zeros((5,5))
a[0,1:]=[0.8,2,3.8,6]
a[1,2:]=[0.9,2.1,3.9]
a[2,3:]=[1.1,2.3];
a[3,4]=1.4
G=nx.DiGraph(a)
p=nx.shortest_path(G,0,4,weight='weight')
d=nx.shortest_path_length(G,0,4,weight='weight')
print('最短路径为:',np.array(p)+1)
print('最小费用为:',d)
2.运行结果