Skip to content
AI360Xpert
Beta
Difficulty: MediumAdvanced Graphs

Cheapest Flights Within K Stops

Problem in Plain English

Each flight links two cities with a price, and k caps how many intermediate stops a trip may use. Return the cheapest fare from the start city to the destination within that limit, or -1 when no such trip exists.

Problem Statement

There are n cities linked by flights, where flights[i] = [from, to, price] is a directed flight costing price. Given a start city src, a destination city dst, and an integer k, return the cheapest price of any trip from src to dst using at most k stops.

A stop is an intermediate city, so k stops means at most k + 1 flights. If no trip respects the limit, return -1.

Constraints

  • 2 <= n <= 100
  • 0 <= flights.length <= (n * (n - 1) / 2)
  • flights[i].length == 3
  • 0 <= from, to < n
  • from != to
  • 1 <= price <= 10^4
  • No two flights share the same (from, to) pair.
  • 0 <= src, dst, k < n
  • src != dst

Examples

Example 1

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

Trip 0 to 1 to 3 costs 700 with one stop, while the cheaper 400 route needs two stops and breaks the limit.

Example 2

Input
n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output
200

Trip 0 to 1 to 2 costs 200 with one stop, beating the direct 500 flight.

Example 3

Input
n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output
500

With zero stops allowed, only the direct 0 to 2 flight qualifies.

Intuition

The cheapest arrival at a city is not enough information: it may have spent too many flights to continue. In Example 1, 0 to 1 to 2 to 3 costs only 400 but uses three flights, while the two-flight budget permits 0 to 1 to 3 for 700. Enumerate budget-limited walks, or share their work by remembering the cheapest arrival at each city after each allowed flight count. A relaxation round extends only the previous round, so it cannot accidentally spend extra flights.

Approaches

DFS Every Route

Solution Details

Reveal DFS Every Route: intuition, complexity, and code

Hints

Hint 1
Plain shortest-path search ignores the stop budget, so a cheaper trip with too many stops can beat a valid one.
Hint 2
Exactly k + 1 flights fit the budget, which matches one extra flight of reach per relaxation round.
Hint 3
Copy the prices before each round so a round only extends trips that were already valid in the previous round.

Edge Cases

  • Start and destination disconnected, so prices stay infinite and the answer is -1.
  • k = 0, where only a direct flight qualifies and a single round decides the answer.
  • A cheap route needing too many stops, which must lose to a pricier valid route as in Example 1.
  • An empty flight list returns -1 because src and dst differ.
  • Directed flights cannot be traversed backward; cycles consume budget and positive fare without helping.
  • A route using exactly k + 1 flights is valid, including an arrival with zero budget left.

Common Mistakes and Interview Tips

  • Running plain Dijkstra without stop tracking, which returns the cheapest trip regardless of its stop count.
  • Relaxing into the same array being read, which chains several flights inside one round and smuggles in over-budget trips.
  • Looping k times instead of k + 1, which prices trips with one flight too few.
  • Marking a city globally visited in DFS loses arrivals with different costs or remaining budgets.
  • Interview tip: explain the at-most-flight-count invariant and why copying preserves shorter paths; a cheaper city arrival with more flights used does not dominate every other arrival.

Key Takeaway

When a shortest path also caps the edge count, limit the relaxation rounds to the cap instead of running the textbook algorithm to convergence.