Question DSA-GRAPH-0043
MediumWhat is the output of this code?
import heapq
g = {"S": [("A", 4), ("B", 1)], "B": [("A", 2), ("C", 5)], "A": [("C", 1)], "C": []}
d = {"S": 0}
pq = [(0, "S")]
while pq:
du, u = heapq.heappop(pq)
if du > d[u]:
continue
for v, w in g[u]:
if du + w < d.get(v, 10**9):
d[v] = du + w
heapq.heappush(pq, (d[v], v))
print(d["A"], d["C"])
- A.
4 6
- B.
3 6
- C.
Sign up free to see the answer and a step-by-step explanation.