787. K 站中转内最便宜的航班

的用户
题目链接:https://leetcode-cn.com/problems/cheapest-flights-within-k-stops/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
func findCheapestPrice(_ n: Int, _ flights: [[Int]], _ src: Int, _ dst: Int, _ k: Int) -> Int {
let maxDist = Int.max/2
var dist = [Int](repeating: maxDist, count: n)
dist[src] = 0
for limit in 0 ... k {
var clone = dist
for f in flights {
var x = f[0], y = f[1], w = f[2]
dist[y] = min(dist[y], clone[x]+w)
}
}
let ans = dist[dst]
return ans > maxDist/2 ? -1 : ans
}
}

787. K 站中转内最便宜的航班
https://pisces34.github.io/2021/08/24/leetcode/787/
发布于
2021年8月24日
许可协议