Graph Theory37 sections · 1633 units
Open in Course

LeetCode 787 Cheapest Flights Within K Stops - State Definition

Expanded state space

Dijkstra tracks the best cost to reach each position. In a normal graph, position is the entire state. Here, position alone is not enough. Why?

Because arriving at city 55 with 00 stops left is different from arriving at city 55 with 33 stops left. The first state is a dead end. The second can still explore neighbors. Your state is a pair: (city, stops_remaining). You track the best cost to reach each (city, k) pair. That's how you handle the constraint.