Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 4→5 (1), 1→7 (20), 2→3 (2), 0→1 (2), 4→1 (18), 6→7 (3), 3→4 (3), 5→6 (1), 0→4 (10), 1→2 (2). What is the minimum total weight of a path from 0 to 7 that uses AT MOST 3 edges?

  1. A. 14
  2. B. 15
  3. C. 48
  4. D. 22

Correct answer: D. 22

Dynamic programming over the number of edges gives 22. The unconstrained shortest path (14) uses too many edges, allowing 4 edges gives 15, and insisting on exactly 3 edges gives 48.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic