← 返回 snowflake 的题目列表Cheapest Flights Within K Stops
类型:qbank
There are n cities connected by some flights.
Cheapest Flights Within K Stops
There are n cities connected by some flights.
SWE
graph
shortest-path
bellman-ford
bfs
medium
Frequency
Single report
Last asked
2026-02-03
Stage
phone-screen · onsite-coding
Cheapest Flights Within K Stops
Problem Explanation
Imagine you have n cities that are connected by airplane flights. You are given a list called flights. Each item in this list is an array that looks like [from, to, price]. This represents a flight departing from city from, arriving at city to, and costing price.
You are also provided with three specific inputs:
src: The starting city.
dst: The destination city.
k: The maximum number of stops allowed.
Your Goal: Find the lowest cost to travel from src to dst. However, your route cannot have more than k stops. If it is impossible to reach the destination following these rules, return -1.
Test Cases
Case 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
Case 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
Technical 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