Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional
Directed graph on vertices 0 to 7 with non-negative weights (u→v (w)): 3→4 (3), 0→6 (18), 6→7 (2), 4→5 (3), 0→2 (15), 1→0 (15), 1→6 (17), 2→6 (12), 2→3 (2), 0→1 (2), 1→2 (3), 1→3 (12), 5→6 (2). What is the minimum total weight of a path from 0 to 7 that uses AT MOST 3 edges?
- A. 20
- B. 17
- C. 19
- D. 21
Correct answer: A. 20
Dynamic programming over the number of edges gives 20. The unconstrained shortest path (17) uses too many edges, allowing 4 edges gives 19, and insisting on exactly 3 edges gives 21.
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 5 with non-negative weights (u→v (w)): 3→0 (19), 0→1 (3), 1→2 (2), 3→4 (1), 3→5 (8), 4→3 (8), 0→3 (14)…
- 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)…
- Directed graph on vertices 0 to 5 with non-negative weights (u→v (w)): 0→4 (17), 0→1 (3), 1→2 (1), 3→5 (10), 3→4 (1), 2→3 (1), 4→5 (1)…
- Directed graph on vertices 0 to 6 with non-negative weights (u→v (w)): 4→3 (11), 4→5 (1), 0→5 (17), 6→3 (12), 0→1 (1), 3→6 (13), 2→3 (3)…
- 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)…
- 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)…