Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

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

  1. A. 7
  2. B. 19
  3. C. 15
  4. D. 23

Correct answer: B. 19

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic