🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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)
  1. A.[0, 4, 5]
  2. B.[0, 2, 5]
  3. C.[0, 1, 5]
  4. 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 →