大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode787. K 站中转内最便宜的航班 | Cheapest Flights Within K Stops,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
There are n
cities connected by @H_91_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_91_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]
,城市标签从 0
到 n
- 1
.[0,n * (n - 1) / 2]
.(src,pricE)
.[1,10000]
.k
范围是 [0,n - 1]
.68ms
@H_673_196@ 1@H_956_197@ class@H_956_197@ Solution { @H_956_197@@H_673_196@ 2@H_956_197@ func findCheapestPrice(_ n: Int,_ flights: [[Int]],_ src: Int,_ dst: Int,_ K: int) -> Int { @H_956_197@@H_673_196@ 3@H_956_197@ var@H_956_197@ grid = [[Int]](repeaTing: [Int](repeaTing: 0@H_956_197@,count: n),count: n) @H_956_197@@H_673_196@ 4@H_956_197@ for@H_956_197@ flight in@H_956_197@ flights { @H_956_197@@H_673_196@ 5@H_956_197@ grid[flight[1@H_956_197@]][flight[0@H_956_197@]] = flight[2@H_956_197@] @H_956_197@@H_673_196@ 6@H_956_197@ } @H_956_197@@H_673_196@ 7@H_956_197@ var@H_956_197@ k = K @H_956_197@@H_673_196@ 8@H_956_197@ var@H_956_197@ dsts = [(dst,0@H_956_197@)],nextDst = [(Int,int)]() @H_956_197@@H_673_196@ 9@H_956_197@ var@H_956_197@ ans = Int.max @H_956_197@@H_673_196@ 10@H_956_197@ while@H_956_197@ dsts.count > 0@H_956_197@ && k >= 0@H_956_197@ { @H_956_197@@H_673_196@ 11@H_956_197@ let (validDst,v) = dsts.removeFirst() @H_956_197@@H_673_196@ 12@H_956_197@ for@H_956_197@ i in@H_956_197@ grid[validDst].inDices { @H_956_197@@H_673_196@ 13@H_956_197@ if@H_956_197@ grid[validDst][i] != 0@H_956_197@ { @H_956_197@@H_673_196@ 14@H_956_197@ if@H_956_197@ i == src { ans = min(ans,grid[validDst][i] + v) } @H_956_197@@H_673_196@ 15@H_956_197@ else@H_956_197@ { @H_956_197@@H_673_196@ 16@H_956_197@ if@H_956_197@ ans >= grid[validDst][i] + v { @H_956_197@@H_673_196@ 17@H_956_197@ nextDst.append((i,grid[validDst][i] + v)) @H_956_197@@H_673_196@ 18@H_956_197@ } @H_956_197@@H_673_196@ 19@H_956_197@ } @H_956_197@@H_673_196@ 20@H_956_197@ } @H_956_197@@H_673_196@ 21@H_956_197@ } @H_956_197@@H_673_196@ 22@H_956_197@ if@H_956_197@ dsts.count == 0@H_956_197@ { @H_956_197@@H_673_196@ 23@H_956_197@ dsts = nextDst @H_956_197@@H_673_196@ 24@H_956_197@ nextDst.removeAll() @H_956_197@@H_673_196@ 25@H_956_197@ k -= 1@H_956_197@ @H_673_196@ 26@H_956_197@ } @H_956_197@@H_673_196@ 27@H_956_197@ } @H_956_197@@H_673_196@ 28@H_956_197@ return@H_956_197@ ans == Int.max ? -1@H_956_197@ : ans @H_956_197@@H_673_196@ 29@H_956_197@ } @H_956_197@@H_673_196@ 30@H_956_197@ @H_673_196@ 31@H_956_197@ func mainBFS(_ n: Int,_ K: int) -> Int { @H_956_197@@H_673_196@ 32@H_956_197@ @H_673_196@ 33@H_956_197@ var@H_956_197@ queue = Queue<City>() @H_956_197@@H_673_196@ 34@H_956_197@ queue.enqueue(srC) @H_956_197@@H_673_196@ 35@H_956_197@ @H_673_196@ 36@H_956_197@ var@H_956_197@ visited = Set<City>() @H_956_197@@H_673_196@ 37@H_956_197@ var@H_956_197@ stops = 0@H_956_197@ @H_673_196@ 38@H_956_197@ var@H_956_197@ cheapestPrice = 0@H_956_197@ @H_673_196@ 39@H_956_197@ @H_673_196@ 40@H_956_197@ let graph = genGraph(flights) @H_956_197@@H_673_196@ 41@H_956_197@ @H_673_196@ 42@H_956_197@ while@H_956_197@ let fromCity = queue.dequeue() { @H_956_197@@H_673_196@ 43@H_956_197@ @H_673_196@ 44@H_956_197@ if@H_956_197@ fromCity == dst { @H_956_197@@H_673_196@ 45@H_956_197@ return@H_956_197@ cheapestPrice @H_956_197@@H_673_196@ 46@H_956_197@ } @H_956_197@@H_673_196@ 47@H_956_197@ @H_673_196@ 48@H_956_197@ if@H_956_197@ stops == K { @H_956_197@@H_673_196@ 49@H_956_197@ //@H_956_197@ check if we can make it to the desTination @H_956_197@@H_673_196@ 50@H_956_197@ //@H_956_197@ or return -1 since we will have exceeded max layovers@H_956_197@ @H_673_196@ 51@H_956_197@ var@H_956_197@ priCEToDst = -1@H_956_197@ @H_673_196@ 52@H_956_197@ if@H_956_197@ let nextCities = graph[fromCity] { @H_956_197@@H_673_196@ 53@H_956_197@ var@H_956_197@ i = 0@H_956_197@ @H_673_196@ 54@H_956_197@ var@H_956_197@ foundDst = false@H_956_197@ @H_673_196@ 55@H_956_197@ while@H_956_197@ i < nextCities.count && !foundDst { @H_956_197@@H_673_196@ 56@H_956_197@ if@H_956_197@ nextCities[i] == dst { @H_956_197@@H_673_196@ 57@H_956_197@ priCEToDst = cheapestPrice + price(flights,from@H_956_197@: fromCity,to: nextCities[i]) @H_956_197@@H_673_196@ 58@H_956_197@ foundDst = true@H_956_197@ @H_673_196@ 59@H_956_197@ } @H_956_197@@H_673_196@ 60@H_956_197@ i += 1@H_956_197@ @H_673_196@ 61@H_956_197@ } @H_956_197@@H_673_196@ 62@H_956_197@ } @H_956_197@@H_673_196@ 63@H_956_197@ return@H_956_197@ priCEToDst @H_956_197@@H_673_196@ 64@H_956_197@ } @H_956_197@@H_673_196@ 65@H_956_197@ @H_673_196@ 66@H_956_197@ //@H_956_197@ Look ahead and choose the next cheapest flight (Dijkstra‘s algorithm) @H_956_197@@H_673_196@ 67@H_956_197@ //@H_956_197@ Important! This only works with positive edge values.@H_956_197@ @H_673_196@ 68@H_956_197@ if@H_956_197@ let toCity = nextCheapestCity(from@H_956_197@: fromCity,graph: graph,flights: flights) { @H_956_197@@H_673_196@ 69@H_956_197@ @H_673_196@ 70@H_956_197@ //@H_956_197@ Don‘t revisit a city we have already traveled to@H_956_197@ @H_673_196@ 71@H_956_197@ if@H_956_197@ !visited.contains(toCity) { @H_956_197@@H_673_196@ 72@H_956_197@ //@H_956_197@ visited.insert(toCity) @H_956_197@@H_673_196@ 73@H_956_197@ @H_673_196@ 74@H_956_197@ //@H_956_197@ Cheapest prices so far@H_956_197@ @H_673_196@ 75@H_956_197@ cheapestPrice += price(flights,to: toCity) @H_673_196@ 76@H_956_197@ @H_673_196@ 77@H_956_197@ //@H_956_197@ Stops@H_956_197@ @H_673_196@ 78@H_956_197@ stops += 1@H_956_197@ @H_673_196@ 79@H_956_197@ @H_673_196@ 80@H_956_197@ //@H_956_197@ Enqueue next city@H_956_197@ @H_673_196@ 81@H_956_197@ queue.enqueue(toCity) @H_956_197@@H_673_196@ 82@H_956_197@ } @H_956_197@@H_673_196@ 83@H_956_197@ } @H_956_197@@H_673_196@ 84@H_956_197@ } @H_956_197@@H_673_196@ 85@H_956_197@ print(@H_545_616@"@H_956_197@@H_545_616@returned here with cheapest price -> \(cheapestPricE)@H_956_197@@H_545_616@"@H_956_197@) @H_956_197@@H_673_196@ 86@H_956_197@ return@H_956_197@ -1@H_956_197@ @H_673_196@ 87@H_956_197@ } @H_956_197@@H_673_196@ 88@H_956_197@ @H_673_196@ 89@H_956_197@ func mainDFS(_ n: Int,_ K: int) -> Int { @H_956_197@@H_673_196@ 90@H_956_197@ var@H_956_197@ minPrice = Int.max @H_956_197@@H_673_196@ 91@H_956_197@ var@H_956_197@ curPrice = 0@H_956_197@ @H_673_196@ 92@H_956_197@ var@H_956_197@ curLayovers = 0@H_956_197@ @H_673_196@ 93@H_956_197@ var@H_956_197@ visited = Set<Int>() @H_956_197@@H_673_196@ 94@H_956_197@ var@H_956_197@ found = false@H_956_197@ @H_673_196@ 95@H_956_197@ let graph = genGraph(flights) @H_956_197@@H_673_196@ 96@H_956_197@ visited.insert(srC) @H_956_197@@H_673_196@ 97@H_956_197@ dfs(flights,dst: dst,k: K,vertex: src,curPrice: &curPrice,curLayovers: &curLayovers,visited: &visited,found: &found,minPrice: &@H_775_200@minPricE) @H_956_197@@H_673_196@ 98@H_956_197@ return@H_956_197@ found ? minPrice : -1@H_956_197@ @H_673_196@ 99@H_956_197@ } @H_956_197@@H_673_196@100@H_956_197@ @H_673_196@101@H_956_197@ func dfs(_ flights: [[Int]],dst: Int,k: Int,graph: [City: [City]],vertex: Int,curPrice: inout Int,curLayovers: inout Int,visited: inout Set<Int>,found: inout Bool,minPrice: inout int) { @H_956_197@@H_673_196@102@H_956_197@ if@H_956_197@ vertex == dst { @H_956_197@@H_673_196@103@H_956_197@ found = true@H_956_197@ @H_673_196@104@H_956_197@ if@H_956_197@ curPrice < minPrice { @H_956_197@@H_673_196@105@H_956_197@ minPrice = curPrice @H_956_197@@H_673_196@106@H_956_197@ } @H_956_197@@H_673_196@107@H_956_197@ return@H_956_197@ @H_673_196@108@H_956_197@ } @H_956_197@@H_673_196@109@H_956_197@ if@H_956_197@ curLayovers > k { @H_956_197@@H_673_196@110@H_956_197@ return@H_956_197@ @H_673_196@111@H_956_197@ } @H_956_197@@H_673_196@112@H_956_197@ @H_673_196@113@H_956_197@ if@H_956_197@ let desTinations = graph[vertex] { @H_956_197@@H_673_196@114@H_956_197@ desTinations.forEach { neighbor in@H_956_197@ @H_673_196@115@H_956_197@ if@H_956_197@ !visited.contains(neighbor) { @H_956_197@@H_673_196@116@H_956_197@ var@H_956_197@ toPrice = price(flights,from@H_956_197@: vertex,to: neighbor) @H_956_197@@H_673_196@117@H_956_197@ curPrice += toPrice @H_956_197@@H_673_196@118@H_956_197@ curLayovers += 1@H_956_197@ @H_673_196@119@H_956_197@ dfs(flights,k: k,vertex: neighbor,minPrice: &@H_775_200@minPricE) @H_956_197@@H_673_196@120@H_956_197@ visited.remove(neighbor) @H_956_197@@H_673_196@121@H_956_197@ curPrice -= toPrice @H_956_197@@H_673_196@122@H_956_197@ curLayovers -= 1@H_956_197@ @H_673_196@123@H_956_197@ } @H_956_197@@H_673_196@124@H_956_197@ } @H_956_197@@H_673_196@125@H_956_197@ } @H_956_197@@H_673_196@126@H_956_197@ @H_673_196@127@H_956_197@ } @H_956_197@@H_673_196@128@H_956_197@ @H_673_196@129@H_956_197@ //@H_956_197@ Helpers@H_956_197@ @H_673_196@130@H_956_197@ @H_673_196@131@H_956_197@ typealias City = Int @H_956_197@@H_673_196@132@H_956_197@ typealias Price = Int @H_956_197@@H_673_196@133@H_956_197@ @H_673_196@134@H_956_197@ struct@H_956_197@ Queue<Element> { @H_956_197@@H_673_196@135@H_956_197@ var@H_956_197@ storage = [Element]() @H_956_197@@H_673_196@136@H_956_197@ mutaTing func enqueue(_ element: Element) { @H_956_197@@H_673_196@137@H_956_197@ self.storage.append(element) @H_956_197@@H_673_196@138@H_956_197@ } @H_956_197@@H_673_196@139@H_956_197@ mutaTing func dequeue() -> Element? { @H_956_197@@H_673_196@140@H_956_197@ guard self.storage.count > 0@H_956_197@ else@H_956_197@ { return@H_956_197@ nil } @H_956_197@@H_673_196@141@H_956_197@ return@H_956_197@ self.storage.removeFirst() @H_956_197@@H_673_196@142@H_956_197@ } @H_956_197@@H_673_196@143@H_956_197@ } @H_956_197@@H_673_196@144@H_956_197@ @H_673_196@145@H_956_197@ func genGraph(_ flights: [[Int]]) -> [City: [City]] { @H_956_197@@H_673_196@146@H_956_197@ var@H_956_197@ graph = [Int: [Int]]() @H_956_197@@H_673_196@147@H_956_197@ flights.forEach { flight in@H_956_197@ @H_673_196@148@H_956_197@ let from@H_956_197@ = flight[0@H_956_197@] @H_956_197@@H_673_196@149@H_956_197@ let to = flight[1@H_956_197@] @H_956_197@@H_673_196@150@H_956_197@ if@H_956_197@ var@H_956_197@ edges = graph[from@H_956_197@] { @H_956_197@@H_673_196@151@H_956_197@ edges.append(to) @H_956_197@@H_673_196@152@H_956_197@ graph[from@H_956_197@] = edges @H_956_197@@H_673_196@153@H_956_197@ } else@H_956_197@ { @H_956_197@@H_673_196@154@H_956_197@ graph[from@H_956_197@] = [to] @H_956_197@@H_673_196@155@H_956_197@ } @H_956_197@@H_673_196@156@H_956_197@ } @H_956_197@@H_673_196@157@H_956_197@ return@H_956_197@ graph @H_956_197@@H_673_196@158@H_956_197@ } @H_956_197@@H_673_196@159@H_956_197@ @H_673_196@160@H_956_197@ func price(_ flights: [[Int]],from@H_956_197@: Int,to: int) -> Int { @H_956_197@@H_673_196@161@H_956_197@ var@H_956_197@ i = 0@H_956_197@ @H_673_196@162@H_956_197@ while@H_956_197@ i < flights.count { @H_956_197@@H_673_196@163@H_956_197@ let flight = flights[i] @H_956_197@@H_673_196@164@H_956_197@ if@H_956_197@ from@H_956_197@ == flight[0@H_956_197@],to == flight[1@H_956_197@] { @H_956_197@@H_673_196@165@H_956_197@ return@H_956_197@ flight[2@H_956_197@] @H_956_197@@H_673_196@166@H_956_197@ } @H_956_197@@H_673_196@167@H_956_197@ i += 1@H_956_197@ @H_673_196@168@H_956_197@ } @H_956_197@@H_673_196@169@H_956_197@ return@H_956_197@ -1@H_956_197@ @H_673_196@170@H_956_197@ } @H_956_197@@H_673_196@171@H_956_197@ @H_673_196@172@H_956_197@ //@H_956_197@ Note: This can be done when creaTing the graph instead for the BFS solution to improve perfoRMANce@H_956_197@ @H_673_196@173@H_956_197@ func nextCheapestCity(from@H_956_197@ city: City,flights: [[Int]]) -> City?