import heapq
def dijkstra(graph, start):
# 初始化距离字典:所有节点距离设为无穷大,起点为0
distances = {node: float('inf') for node in graph}
distances[start] = 0
# 优先队列(最小堆),存储(当前距离, 节点)元组
priority_queue = [(0, start)]
while priority_queue:
# 弹出当前距离最小的节点
current_distance, current_node = heapq.heappop(priority_queue)
# 若当前距离已大于已知最短距离,跳过(避免重复处理)
if current_distance > distances[current_node]:
continue
# 遍历当前节点的邻居
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
# 若找到更短路径,更新距离并加入队列
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(priority_queue, (distance, neighbor))
return distances
05-20
1188

08-04
462

04-25
9958

10-09
1073

07-09