-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path787-CheapestFlightsWithinKStops.go
More file actions
179 lines (161 loc) · 5.79 KB
/
787-CheapestFlightsWithinKStops.go
File metadata and controls
179 lines (161 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
// 787. Cheapest Flights Within K Stops
// There are n cities connected by some number of flights.
// You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.
// You are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops.
// If there is no such route, return -1.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png" / >
// 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
// Explanation:
// The graph is shown above.
// The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
// Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png" / >
// Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
// Output: 200
// Explanation:
// The graph is shown above.
// The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png" / >
// Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
// Output: 500
// Explanation:
// The graph is shown above.
// The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
// Constraints:
// 1 <= n <= 100
// 0 <= flights.length <= (n * (n - 1) / 2)
// flights[i].length == 3
// 0 <= fromi, toi < n
// fromi != toi
// 1 <= pricei <= 10^4
// There will not be any multiple flights between two cities.
// 0 <= src, dst, k < n
// src != dst
import "fmt"
func findCheapestPrice(n int, flights [][]int, src int, dst int, k int) int {
min := func (a, b int) int {
if a < b {
return a
}
return b
}
// 航班的花费不超过 10^4 最多搭乘航班的次数 k+1 不超过 101
const inf = 10000*101 + 1
f := make([]int, n)
for i := range f {
f[i] = inf
}
f[src] = 0
ans := inf
for t := 1; t <= k+1; t++ {
g := make([]int, n)
for i := range g {
g[i] = inf
}
for _, flight := range flights {
j, i, cost := flight[0], flight[1], flight[2]
g[i] = min(g[i], f[j]+cost)
}
f = g
ans = min(ans, f[dst])
}
if ans == inf {
ans = -1
}
return ans
}
func findCheapestPrice1(n int, flights [][]int, src int, dst int, k int) int {
min := func (x, y int) int { if x < y { return x; }; return y; }
// 航班的花费不超过 10^4 最多搭乘航班的次数 k+1 不超过 101
const inf = 10000*101 + 1
dp := make([][]int, k+2)
for i := range dp {
dp[i] = make([]int, n)
for j := range dp[i] {
dp[i][j] = inf
}
}
dp[0][src] = 0
for t := 1; t <= k+1; t++ {
for _, flight := range flights {
j, i, cost := flight[0], flight[1], flight[2]
dp[t][i] = min(dp[t][i], dp[t-1][j]+cost)
}
}
res := inf
for t := 1; t <= k+1; t++ {
res = min(res, dp[t][dst])
}
if res == inf {
res = -1
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png" / >
// 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
// Explanation:
// The graph is shown above.
// The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
// Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
fmt.Println(findCheapestPrice(
4,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{2,0,100},[]int{1,3,600},[]int{2,3,200}}, // flights
0, // src
3, // dst
1, // k
)) // 700
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png" / >
// Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
// Output: 200
// Explanation:
// The graph is shown above.
// The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
fmt.Println(findCheapestPrice(
3,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{0,2,100}}, // flights
0, // src
2, // dst
1, // k
)) // 200
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png" / >
// Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
// Output: 500
fmt.Println(findCheapestPrice(
3,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{0,2,500}}, // flights
0, // src
2, // dst
0, // k
)) // 500
fmt.Println(findCheapestPrice1(
4,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{2,0,100},[]int{1,3,600},[]int{2,3,200}}, // flights
0, // src
3, // dst
1, // k
)) // 700
fmt.Println(findCheapestPrice1(
3,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{0,2,100}}, // flights
0, // src
2, // dst
1, // k
)) // 200
fmt.Println(findCheapestPrice1(
3,
[][]int{[]int{0,1,100},[]int{1,2,100},[]int{0,2,500}}, // flights
0, // src
2, // dst
0, // k
)) // 500
}