def dijkstra(graph, from_node, to_node):
q, seen = [(0, from_node, [])], set()
while q:
cost, node, path = heappop(q)
seen.add(node)
path = path+[node]
if node == to_node:
return cost,path
for adj_node, c in graph.get(node, {}).items():
if adj_node not in seen:
heappush(q, (cost+c, adj_node, path))
return -1,[] air_lines = {"1":{"2":2000, "3":2000, "4":4000, "5": 4500}, "2":{"5": 1000}, "3":{"4": 1000}, "4":{"5": 500}} print(dijkstra(air_lines, "1", "4"))
print(dijkstra(air_lines, "1", "5"))
print(dijkstra(air_lines, "4", "5"))
print(dijkstra(air_lines, "5", "4"))
print(dijkstra(air_lines, "1", "1"))
print(dijkstra(air_lines, "10", "10")) """
(3000, ['1', '3', '4'])
(3000, ['1', '2', '5'])
(500, ['4', '5'])
(-1, [])
(0, ['1'])
(0, ['10'])
"""

  

最新文章

  1. C# Struct结构体里数组长度的指定
  2. shell 脚本编程概述
  3. 改变DataGrid某一行和单元格的颜色
  4. 使用WebView视图显示网页-----迷你浏览器
  5. android中string.xml引起的常见编译错误
  6. 关于asp:login控件和验证码的问题?(转)
  7. 在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败
  8. centos7 搭建elk
  9. MySQL数据库面试
  10. iptables snat和dnat
  11. asp.net core系列 28 EF模型配置(字段,构造函数,拥有实体类型)
  12. 关于新写的js在浏览器f12的时候看不到解决办法
  13. mongo 让字段自增或自减
  14. ACM-ICPC 2018 南京赛区网络预赛 J题Sum(线性筛素数)
  15. ArcGIS for qml -设置地图和视域中心
  16. 反射-Emit
  17. 私有仓库GitLab快速入门篇
  18. 第二篇:Jmeter功能概要
  19. bootstrapTable刷新当前页码不变和从第一页开始查询
  20. Angularjs 首次加载显示{{}}

热门文章

  1. 查看Oracle表空间以及用户与其默认表空间情况
  2. keystone源码阅读--python函数
  3. [转帖]LINUX PID 1 和 SYSTEMD
  4. 手撕面试官系列(五):Tomcat+Mysql+设计模式面试专题
  5. Python-07-高阶函数
  6. python使用自带模块httplib进行http请求
  7. Hadoop—MapReduce计算气象温度
  8. maven引入外部包的方法1
  9. 在Centos6.5上部署kvm虚拟化技术
  10. Matlab原型模式