Data Structures & AlgorithmsModerateGATE CS · ICPC regional

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

  1. A. 0
  2. B. 1
  3. C. -3
  4. D. 8

Correct answer: C. -3

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic