Cheapest Flights Within K Stops
There are n cities connected by some flights where flights[i] = [from, to, price]. Given src, dst, and an integer k, return the cheapest price to fly from src to dst using at most k stops (i.e. at most k+1 flights). If there is no such route, return -1.
Open official problem prompt ↗Find the cheapest airfare from src to dst that uses no more than k intermediate stops.
Like booking a trip on a budget where each layover uses up an allowance: you want the cheapest ticket but you can only tolerate up to k connections.
- Input
- n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
- Output
- 700
- Why
- 0->1->3 costs 100+600=700 using 1 stop; the cheaper 0->1->2->3 (400) needs 2 stops, exceeding k.
1 <= n <= 1000 <= flights.length <= n*(n-1)/2flights[i] = [from_i, to_i, price_i]0 <= from_i, to_i < nfrom_i != to_i1 <= price_i <= 10^40 <= src, dst, k < nsrc != dst