Data Structures & AlgorithmsModerateGATE CS · ICPC regional

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

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

Correct answer: B. 4

Plain Bellman–Ford with k rounds (relaxing only through paths of length at most 2) gives 4. Ignoring the limit gives 1, and only paths of exactly 2 arcs give 6.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic