Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

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

  1. A. 8
  2. B. 14
  3. C. 19
  4. D. 35

Correct answer: C. 19

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic