Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A directed graph with no directed cycles has arcs u→v (weight) : U→C (-1), U→Z (6), U→H (8), U→K (8), C→Z (-2), C→H (-1), Z→H (5), Z→K (-1), H→K (0). 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. 4
  2. B. 6
  3. C. 5
  4. D. 8

Correct answer: C. 5

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic