There are n cities labeled 0 through n - 1 and directed flights flights[i] = [from_i, to_i, price_i]. Return the cheapest price from src to dst using at most k stops, where a stop is an intermediate city. If no such route exists, return -1.
Input / output
n: int, flights: int[][], src: int, dst: int, k: int-1 if unreachable under the stop limitExamples
n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1 returns 700.n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1 returns 200.k = 0 returns 500 because only direct flights are allowed.Constraints
1 <= n <= 1000 <= flights.length <= n * (n - 1) / 20 <= from_i, to_i < n, from_i != to_i1 <= price_i <= 10^40 <= k < nTarget complexity
O(k * flights.length) time.Hints
k stops, any valid route uses at most k + 1 flight edges.Follow-up Why can a plain Dijkstra implementation return the wrong answer if it ignores how many stops were used to reach a city?