n cities, flights [from,to,price]. Start at src, reach dst with at most k stops. Return cheapest price or -1.
n=4, src=0, dst=3, k=1, flights=[[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]]700Round i = paths with ≤i edges. Clone ensures we only use prev-round dist when relaxing. Stops same-round multi-hop.
Plain Dijkstra finds the cheapest route, but it ignores the limit on stops. We need to control how many flights a route uses.
At most k stops means at most k plus 1 flights. Bellman Ford relaxes every flight once per round, and after r rounds it knows the best prices using at most r flights. So run exactly k plus 1 rounds. In each round, read prices from a copy of the previous round, so one round can never chain two flights together.
Round r of Bellman Ford means at most r edges. Relax from a copy, so a single round never uses two flights.
Run the animation above on your own input, or watch the 2:14 video explanation of Cheapest Flights Within K Stops with a dry run and Java code.