← Quiz: Shortest Paths: all 10 questions
Quiz: Shortest Paths · Question 7 of 10
What does this print? (code)
What does this print?
edges = [(0, 1, 4), (0, 2, 5), (2, 1, -3)]
dist = [0, float('inf'), float('inf')]
for _ in range(2):
for u, v, w in edges:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
print(dist)
- A.[0, 4, 5]
- B.[0, 2, 5]
- C.[0, 1, 5]
- D.[0, 4, 2]
Answer & explanation
Correct answer: [0, 2, 5]
0 → 2 → 1 costs 5 + (−3) = 2, which beats the direct edge of weight 4.
Can you get the rest right without peeking?
Take the timed Quiz: Shortest Paths quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →