Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B

A directed graph with no directed cycles has arcs u→v (weight) : U→A (-3), U→V (4), A→Z (3), V→O (2), Z→O (5). Weights may be negative. What is the minimum total weight of a path from U to O that uses at most 2 arcs?

  1. A. 6
  2. B. 5
  3. C. 0
  4. D. -3

Correct answer: A. 6

Run exactly two Bellman–Ford rounds from a copy of the previous distances: U→V→O = 4 + 2 = 6. The 3-arc path U→A→Z→O = 5 is not allowed, though in-place relaxation can leak it in; 0 stops at Z and −3 is the first arc alone.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic