Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 3→4 (2), 1→0 (15), 4→5 (1), 4→3 (18), 2→3 (2), 6→7 (3), 0→7 (20), 3→7 (14), 3→1 (11), 1→2 (2), 0→4 (9), 0→1 (1), 5→6 (3). What is the minimum total weight of a path from 0 to 7 that uses AT MOST 3 edges?
- A. 14
- B. 20
- C. 16
- D. 36
Correct answer: B. 20
Dynamic programming over the number of edges gives 20. The unconstrained shortest path (14) uses too many edges, allowing 4 edges gives 16, and insisting on exactly 3 edges gives 36.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 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)…
- Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 2→1 (16), 7→4 (4), 3→4 (1), 5→6 (3), 2→3 (2), 5→3 (13), 0→2 (18)…
- Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 1→2 (1), 3→4 (1), 6→7 (2), 0→6 (14), 6→3 (15), 5→6 (2), 4→5 (3)…
- 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)…
- Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 2→3 (3), 2→0 (4), 4→5 (1), 1→2 (2), 2→1 (15), 0→1 (2), 0→3 (11)…
- 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)…