大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode787. K 站中转内最便宜的航班 | Cheapest Flights Within K Stops,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
There are n
cities connected by @H_189_19@m flights. Each fight starts from city u
and arrives at v
with a price w
.
Now given all the cities and flights,together with starTing city src
and the desTination dst
,your task is to find the cheapest price from src
to dst
with up to k
stops. If there is no such route,output -1
.
Example 1: Input: n = 3,edges = [[0,1,100],[1,2,[0,500]] src = 0,dst = 2,k = 1 Output: 200 Explanation: The graph looks like this:
The cheapest price from city to city with at most 1 stop costs 200,as marked red in the picture.02
Example 2: Input: n = 3,k = 0 Output: 500 Explanation: The graph looks like this:
The cheapest price from city to city with at most 0 stop costs 500,as marked blue in the picture.02
Note:
n
will be in range [1,100]
,with nodes labeled from 0
to n
- 1
.flights
will be in range [0,n * (n - 1) / 2]
.(src,
dst
,pricE)
.[1,10000]
.k
is in the range of [0,n - 1]
.有 n
个城市通过 @H_189_19@m 个航班连接。每个航班都从城市 u
开始,以价格 w
抵达 v
。
现在给定所有的城市和航班,以及出发城市 src
和目的地 dst
,你的任务是找到从 src
到 dst
最多经过 k
站中转的最便宜的价格。 如果没有这样的路线,则输出 -1
。
示例 1: 输入: n = 3,k = 1 输出: 200 解释: 城市航班图如下
从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。
示例 2: 输入: n = 3,k = 0 输出: 500 解释: 城市航班图如下
从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。
提示:
n
范围是 [1,100]
,城市@L_673_6@从 0
到 n
- 1
.[0,n * (n - 1) / 2]
.(src,pricE)
.[1,10000]
.k
范围是 [0,n - 1]
.68ms
1@H_947_197@ class@H_947_197@ Solution {
@H_947_197@ 2@H_947_197@ func findCheapestPrice(_ n: Int,_ flights: [[Int]],_ src: Int,_ dst: Int,_ K: int) -> Int {
@H_947_197@ 3@H_947_197@ var@H_947_197@ grid = [[Int]](repeaTing: [Int](repeaTing: 0@H_947_197@,count: n),count: n)
@H_947_197@ 4@H_947_197@ for@H_947_197@ flight in@H_947_197@ flights {
@H_947_197@ 5@H_947_197@ grid[flight[1@H_947_197@]][flight[0@H_947_197@]] = flight[2@H_947_197@]
@H_947_197@ 6@H_947_197@ }
@H_947_197@ 7@H_947_197@ var@H_947_197@ k = K
@H_947_197@ 8@H_947_197@ var@H_947_197@ dsts = [(dst,0@H_947_197@)],nextDst = [(Int,int)]()
@H_947_197@ 9@H_947_197@ var@H_947_197@ ans = Int.max
@H_947_197@ 10@H_947_197@ while@H_947_197@ dsts.count > 0@H_947_197@ && k >= 0@H_947_197@ {
@H_947_197@ 11@H_947_197@ let (validDst,v) = dsts.removeFirst()
@H_947_197@ 12@H_947_197@ for@H_947_197@ i in@H_947_197@ grid[validDst].inDices {
@H_947_197@ 13@H_947_197@ if@H_947_197@ grid[validDst][i] != 0@H_947_197@ {
@H_947_197@ 14@H_947_197@ if@H_947_197@ i == src { ans = min(ans,grid[validDst][i] + v) }
@H_947_197@ 15@H_947_197@ else@H_947_197@ {
@H_947_197@ 16@H_947_197@ if@H_947_197@ ans >= grid[validDst][i] + v {
@H_947_197@ 17@H_947_197@ nextDst.append((i,grid[validDst][i] + v))
@H_947_197@ 18@H_947_197@ }
@H_947_197@ 19@H_947_197@ }
@H_947_197@ 20@H_947_197@ }
@H_947_197@ 21@H_947_197@ }
@H_947_197@ 22@H_947_197@ if@H_947_197@ dsts.count == 0@H_947_197@ {
@H_947_197@ 23@H_947_197@ dsts = nextDst
@H_947_197@ 24@H_947_197@ nextDst.removeAll()
@H_947_197@ 25@H_947_197@ k -= 1@H_947_197@
26@H_947_197@ }
@H_947_197@ 27@H_947_197@ }
@H_947_197@ 28@H_947_197@ return@H_947_197@ ans == Int.max ? -1@H_947_197@ : ans
@H_947_197@ 29@H_947_197@ }
@H_947_197@ 30@H_947_197@
31@H_947_197@ func mainBFS(_ n: Int,_ K: int) -> Int {
@H_947_197@ 32@H_947_197@
33@H_947_197@ var@H_947_197@ queue = Queue<City>()
@H_947_197@ 34@H_947_197@ queue.enqueue(srC)
@H_947_197@ 35@H_947_197@
36@H_947_197@ var@H_947_197@ visited = Set<City>()
@H_947_197@ 37@H_947_197@ var@H_947_197@ stops = 0@H_947_197@
38@H_947_197@ var@H_947_197@ cheapestPrice = 0@H_947_197@
39@H_947_197@
40@H_947_197@ let graph = genGraph(flights)
@H_947_197@ 41@H_947_197@
42@H_947_197@ while@H_947_197@ let fromCity = queue.dequeue() {
@H_947_197@ 43@H_947_197@
44@H_947_197@ if@H_947_197@ fromCity == dst {
@H_947_197@ 45@H_947_197@ return@H_947_197@ cheapestPrice
@H_947_197@ 46@H_947_197@ }
@H_947_197@ 47@H_947_197@
48@H_947_197@ if@H_947_197@ stops == K {
@H_947_197@ 49@H_947_197@ //@H_947_197@ check if we can make it to the desTination
@H_947_197@ 50@H_947_197@ //@H_947_197@ or return -1 since we will have exceeded max layovers@H_947_197@
51@H_947_197@ var@H_947_197@ priCEToDst = -1@H_947_197@
52@H_947_197@ if@H_947_197@ let nextCities = graph[fromCity] {
@H_947_197@ 53@H_947_197@ var@H_947_197@ i = 0@H_947_197@
54@H_947_197@ var@H_947_197@ foundDst = false@H_947_197@
55@H_947_197@ while@H_947_197@ i < nextCities.count && !foundDst {
@H_947_197@ 56@H_947_197@ if@H_947_197@ nextCities[i] == dst {
@H_947_197@ 57@H_947_197@ priCEToDst = cheapestPrice + price(flights,from@H_947_197@: fromCity,to: nextCities[i])
@H_947_197@ 58@H_947_197@ foundDst = true@H_947_197@
59@H_947_197@ }
@H_947_197@ 60@H_947_197@ i += 1@H_947_197@
61@H_947_197@ }
@H_947_197@ 62@H_947_197@ }
@H_947_197@ 63@H_947_197@ return@H_947_197@ priCEToDst
@H_947_197@ 64@H_947_197@ }
@H_947_197@ 65@H_947_197@
66@H_947_197@ //@H_947_197@ Look ahead and choose the next cheapest flight (Dijkstra‘s algorithm)
@H_947_197@ 67@H_947_197@ //@H_947_197@ Important! This only works with positive edge values.@H_947_197@
68@H_947_197@ if@H_947_197@ let toCity = nextCheapestCity(from@H_947_197@: fromCity,graph: graph,flights: flights) {
@H_947_197@ 69@H_947_197@
70@H_947_197@ //@H_947_197@ Don‘t revisit a city we have already traveled to@H_947_197@
71@H_947_197@ if@H_947_197@ !visited.contains(toCity) {
@H_947_197@ 72@H_947_197@ //@H_947_197@ visited.insert(toCity)
@H_947_197@ 73@H_947_197@
74@H_947_197@ //@H_947_197@ Cheapest prices so far@H_947_197@
75@H_947_197@ cheapestPrice += price(flights,to: toCity)
76@H_947_197@
77@H_947_197@ //@H_947_197@ Stops@H_947_197@
78@H_947_197@ stops += 1@H_947_197@
79@H_947_197@
80@H_947_197@ //@H_947_197@ Enqueue next city@H_947_197@
81@H_947_197@ queue.enqueue(toCity)
@H_947_197@ 82@H_947_197@ }
@H_947_197@ 83@H_947_197@ }
@H_947_197@ 84@H_947_197@ }
@H_947_197@ 85@H_947_197@ print(@H_770_616@"@H_947_197@@H_770_616@returned here with cheapest price -> \(cheapestPricE)@H_947_197@@H_770_616@"@H_947_197@)
@H_947_197@ 86@H_947_197@ return@H_947_197@ -1@H_947_197@
87@H_947_197@ }
@H_947_197@ 88@H_947_197@
89@H_947_197@ func mainDFS(_ n: Int,_ K: int) -> Int {
@H_947_197@ 90@H_947_197@ var@H_947_197@ minPrice = Int.max
@H_947_197@ 91@H_947_197@ var@H_947_197@ curPrice = 0@H_947_197@
92@H_947_197@ var@H_947_197@ curLayovers = 0@H_947_197@
93@H_947_197@ var@H_947_197@ visited = Set<Int>()
@H_947_197@ 94@H_947_197@ var@H_947_197@ found = false@H_947_197@
95@H_947_197@ let graph = genGraph(flights)
@H_947_197@ 96@H_947_197@ visited.insert(srC)
@H_947_197@ 97@H_947_197@ dfs(flights,dst: dst,k: K,vertex: src,curPrice: &curPrice,curLayovers: &curLayovers,visited: &visited,found: &found,minPrice: &@H_643_200@minPricE)
@H_947_197@ 98@H_947_197@ return@H_947_197@ found ? minPrice : -1@H_947_197@
99@H_947_197@ }
@H_947_197@100@H_947_197@
101@H_947_