← 返回 uber 的题目列表Cheapest Flights Within K Stops
类型:qbank
There are n cities connected by directed flights with prices. Given a source, destination, and a stop budget k, return the cheapest price from src to dst using at most k stops, or -1 if no such route exists.
Cheapest Flights Within K Stops
There are n cities connected by directed flights with prices. Given a source, destination, and a stop budget k, return the cheapest price from src to dst using at most k stops, or -1 if no such route exists.
SWE
graph
bellman-ford
dijkstra
bfs
shortest-path
medium
Frequency
Single report
Last asked
2026-01-28
Stage
phone-screen
Cheapest Flights Within K Stops
There are n cities connected by some flights. You are given an array flights where flights[i] = [from_i, to_i, price_i] indicates a flight from city from_i to city to_i with cost price_i.
Given 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.
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
Example 2:
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 2
Output: 400
Constraints
1 <= n <= 100
0 <= flights.length <= n * (n - 1) / 2
flights[i].length == 3
0 <= from_i, to_i < n
1 <= price_i <= 10^4
0 <= k < n