You are given a directed weighted graph representing flights between cities.
n
: number of cities, labeled
0..n-1
.
flights
, where each element is
[from, to, price]
meaning there is a flight from city
from
to city
to
that costs
price
.
src
and
dst
: the start and destination cities.
K
: the maximum number of
stops
allowed, where a stop is an intermediate city between
src
and
dst
.
K+1
flights (edges).
Return the minimum total cost to travel from src to dst using at most K stops. If it is not possible, return -1.
price
values are non-negative integers.
flights
.
n = 4
flights = [[0,1,100],[1,2,100],[2,3,100],[0,3,500]]
src = 0, dst = 3, K = 1
The cheapest valid route is 0 -> 1 -> 2 -> 3 is not allowed (2 stops), so the answer is 500 via 0 -> 3.