Technique
BFS/Dijkstra on an Augmented State Graph
Make each node (vertex, extra state) — keys held, stops used, coupon used, visited mask — and run BFS/Dijkstra on that larger graph.
0/10 solved
Recognise it
The signal
shortest path with a resource (k stops, one discount, keys)
1Brute force
DFS over all paths tracking the state.
2The observation
Exponential paths: states with the same (vertex, extra) are interchangeable.
3Why it works
Each augmented state is settled once with its true optimal cost.
Reach for it when
- +visiting all nodes (vertex, mask)
- +puzzle states (lock combinations)
Not when
- ×extra state is too large (needs a different model)
- ×state is monotone and can be pruned greedily
Practice
- Flight DiscountCSESMedium
- Cheapest Flights Within K StopsAmazon, UberMedium
- Swap GameCSESMedium
- Cat and MouseHard
- Cat and Mouse IIHard
- Bus RoutesUber, GoogleHard
- Shortest Path Visiting All NodesHard
- Minimum PathCodeforces2400
- Paired PaymentCodeforces2200
- Fixed Length Walk QueriesCSESHard
Pitfalls
Misconceptions
- !pruning by vertex alone loses states with different resources
Go deeper3 combinations · resources
Combinations
Dijkstra (Weighted Shortest Path)+BFS/Dijkstra on an Augmented State Graph
one discount, at most k stops, fuel, keys
- Dijkstra (Weighted Shortest Path):
- weighted shortest path
- BFS/Dijkstra on an Augmented State Graph:
- duplicates vertices per resource state
0-1 BFS+BFS/Dijkstra on an Augmented State Graph
minimum modifications to make a path exist
- 0-1 BFS:
- 0-cost and 1-cost moves
- BFS/Dijkstra on an Augmented State Graph:
- grid cell plus direction/change state
Minimum Cost to Make at Least One Valid Path in a GridMinimum Obstacle Removal to Reach Corner
Games on Graphs (retrograde analysis)+Topological Sort+BFS/Dijkstra on an Augmented State Graph
chase game on a graph or grid with possible draws
- Games on Graphs (retrograde analysis):
- labels propagate back from terminal states
- Topological Sort:
- Kahn-style degree counters decide LOSE once every move is known to lose
- BFS/Dijkstra on an Augmented State Graph:
- states are (mouse, cat, turn)