def dijkstra(s, t, g):
q = []
dis = collections.defaultdict(lambda: 0x7fffffff) # [0x7fffffff] * len(g)
vis = collections.defaultdict(bool) # [False] * len(g)
dis[s] = 0
heapq.heappush(q, Node(s, 0))
while q:
cur = heapq.heappop(q).to
if vis[cur]: continue
vis[cur] = True
for to, val in g[cur]:
if not vis[to] and dis[cur] + val < dis[to]:
dis[to] = dis[cur] + val
heapq.heappush(q, Node(to, dis[to]))
return dis
评论列表
文章目录