Data Structures & AlgorithmsModerateCodeforces Div 2 D · ICPC regional

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

  1. A. 15
  2. B. 17
  3. C. 19
  4. D. 39

Correct answer: C. 19

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic