 All Problems
Cheapest Flights Within K Stops
medium
graph
dynamic programming
shortest path
bfs
amazon
google
facebook
airbnb

There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicating there is a flight from city fromi to toi with cost pricei.

Given n, flights, src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.

Example 1:

Input:
4 3
0 1 100
1 2 100
2 0 100
1 3 600
2 3 200
src=0 dst=3 k=1
Output: 700

Input format: First line: n numFlights. Next numFlights lines: from to price. Last line: src dst k.

Output format: Cheapest cost or -1.

Run to check your code against the sample cases, or submit to run every case