Data Structures & AlgorithmsVery hardCodeforces Div 2 B · GATE CS
Directed graph on vertices 0 to 7; every edge costs 0 or 1: 4→3 (1), 5→1 (0), 5→2 (0), 7→1 (0), 1→0 (0), 3→4 (1), 7→4 (0), 2→3 (1), 4→7 (0), 4→1 (1), 6→3 (1), 7→5 (1), 5→0 (0), 1→2 (1), 0→1 (0), 4→2 (0), 0→2 (1), 6→1 (1), 4→6 (1), 6→7 (1), 3→1 (0), 0→5 (1), 0→3 (1), 5→6 (0). What is the minimum total cost of a path from 0 to 7, where a path's cost is the sum of its edge costs?
- A. 2
- B. 0
- C. 4
- D. 3
Correct answer: A. 2
A 0-1 BFS (or Dijkstra) gives cost 2. Counting hops gives 3, and forbidding the 0-cost edges gives 4; both are traps.
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; every edge costs 0 or 1: 4→2 (0), 5→0 (1), 2→3 (1), 4→3 (1), 1→0 (1), 1→5 (0), 0→2 (1), 3→2 (1), 5→2…
- Directed graph on vertices 0 to 7; every edge costs 0 or 1: 0→5 (1), 6→3 (0), 0→2 (1), 6→4 (1), 6→2 (0), 4→3 (0), 4→2 (1), 3→0 (0), 3→4…
- Directed graph on vertices 0 to 7; every edge costs 0 or 1: 0→3 (0), 5→6 (1), 4→0 (0), 4→1 (1), 4→6 (1), 2→0 (0), 0→2 (0), 2→4 (0), 2→3…
- Directed graph on vertices 0 to 8; every edge costs 0 or 1: 3→8 (1), 7→3 (1), 2→0 (1), 4→2 (0), 6→2 (0), 6→4 (1), 5→6 (1), 0→2 (1), 4→6…
- Directed graph on vertices 0 to 8; every edge costs 0 or 1: 2→3 (1), 4→2 (1), 4→0 (1), 3→5 (0), 7→4 (1), 7→5 (1), 1→3 (1), 4→1 (0), 8→7…
- Directed graph on vertices 0 to 6; every edge costs 0 or 1: 4→5 (0), 5→0 (0), 2→3 (0), 3→5 (0), 0→1 (1), 5→3 (0), 3→1 (0), 2→6 (0), 4→2…