import heapq
def dijkstra(g, s, e):
d, q = {v: float('inf') for v in g}, [(0, s)]
d[s] = 0
while q:
dist, v = heapq.heappop(q)
if v == e: return dist
if dist > d[v]: continue
for n, w in g[v].items():
if (nd := dist + w) < d[n]:
d[n] = nd
heapq.heappush(q, (nd, n))
return d[e]
g = {'A': {'B': 3}, 'B': {'A': 3, 'C': 1, 'D': 2, 'E': 6}, 'C': {'B': 1, 'D': 3, 'E': 3}, 'D': {'B': 2, 'C': 3, 'E': 3}, 'E': {'B': 6, 'C': 3, 'D': 3}}
print(dijkstra(g, 'A', 'E'))