@@ -131,5 +131,122 @@ public int findCheapestPrice3(int n, int[][] flights, int src, int dst, int K) {
|
131 | 131 | return -1;
|
132 | 132 | }
|
133 | 133 |
|
| 134 | + |
| 135 | +/** |
| 136 | +* https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/128776/5-ms-AC-Java-Solution-based-on-Dijkstra's-Algorithm |
| 137 | +*/ |
| 138 | +private class City implements Comparable<City>{ |
| 139 | +int id; |
| 140 | +int costFromSrc; |
| 141 | +int stopFromSrc; |
| 142 | +public City(int id, int costFromSrc, int stopFromSrc){ |
| 143 | +this.id = id; |
| 144 | +this.costFromSrc = costFromSrc; |
| 145 | +this.stopFromSrc = stopFromSrc; |
| 146 | +} |
| 147 | +public boolean equals(City c){ |
| 148 | +if(c instanceof City) |
| 149 | +return this.id == c.id; |
| 150 | +return false; |
| 151 | +} |
| 152 | +public int compareTo(City c){ |
| 153 | +return this.costFromSrc - c.costFromSrc; |
| 154 | +} |
| 155 | +} |
| 156 | + |
| 157 | +public int findCheapestPrice4(int n, int[][] flights, int src, int dst, int K) { |
| 158 | +int[][] srcToDst = new int[n][n]; |
| 159 | +for(int i = 0; i < flights.length; i++) |
| 160 | +srcToDst[flights[i][0]][flights[i][1]] = flights[i][2]; |
| 161 | + |
| 162 | +PriorityQueue<City> minHeap = new PriorityQueue(); |
| 163 | +minHeap.offer(new City(src,0,0)); |
| 164 | + |
| 165 | +int[] cost = new int[n]; |
| 166 | +Arrays.fill(cost, Integer.MAX_VALUE); |
| 167 | +cost[src] = 0; |
| 168 | +int[] stop = new int[n]; |
| 169 | +Arrays.fill(stop, Integer.MAX_VALUE); |
| 170 | +stop[src] = 0; |
| 171 | + |
| 172 | +while(!minHeap.isEmpty()){ |
| 173 | +City curCity = minHeap.poll(); |
| 174 | +if(curCity.id == dst) return curCity.costFromSrc; |
| 175 | +if(curCity.stopFromSrc == K + 1) continue; |
| 176 | +int[] nexts = srcToDst[curCity.id]; |
| 177 | +for(int i = 0; i < n; i++){ |
| 178 | +if(nexts[i] != 0){ |
| 179 | +int newCost = curCity.costFromSrc + nexts[i]; |
| 180 | +int newStop = curCity.stopFromSrc + 1; |
| 181 | +if(newCost < cost[i]){ |
| 182 | +minHeap.offer(new City(i, newCost, newStop)); |
| 183 | +cost[i] = newCost; |
| 184 | +} |
| 185 | +else if(newStop < stop[i]){ |
| 186 | +minHeap.offer(new City(i, newCost, newStop)); |
| 187 | +stop[i] = newStop; |
| 188 | +} |
| 189 | +} |
| 190 | +} |
| 191 | +} |
| 192 | + |
| 193 | +return cost[dst] == Integer.MAX_VALUE? -1:cost[dst]; |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | +/** |
| 198 | +* https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/163698/easy-java-Bellman-Ford |
| 199 | +*/ |
| 200 | +public int findCheapestPrice5(int n, int[][] flights, int src, int dst, int k) { |
| 201 | +int INF = 0x3F3F3F3F; |
| 202 | +int[] cost = new int[n]; |
| 203 | +Arrays.fill(cost, INF); |
| 204 | +cost[src] = 0; |
| 205 | +int ans = cost[dst]; |
| 206 | +for(int i = k; i >= 0; i--){ |
| 207 | +int[] cur = new int[n]; |
| 208 | +Arrays.fill(cur, INF); |
| 209 | +for(int[] flight : flights){ |
| 210 | +cur[flight[1]] = Math.min(cur[flight[1]], cost[flight[0]] + flight[2]); |
| 211 | +} |
| 212 | +cost = cur; |
| 213 | +ans = Math.min(ans, cost[dst]); |
| 214 | +} |
| 215 | +return ans == INF ? -1 : ans; |
| 216 | +} |
| 217 | + |
| 218 | + |
| 219 | +private static int MAX = Integer.MAX_VALUE / 2; |
| 220 | +public int findCheapestPrice6(int n, int[][] flights, int src, int dst, int K) { |
| 221 | +int[][] dp = new int[K+2][n]; |
| 222 | +for (int[] row: dp) Arrays.fill(row, MAX); |
| 223 | +dp[0][src] = 0; |
| 224 | +for (int i=1; i<=K+1; i++) { |
| 225 | +for (int[] f: flights) { |
| 226 | +dp[i][f[1]] = Math.min(dp[i][f[1]], Math.min(dp[i-1][f[1]], |
| 227 | +dp[i-1][f[0]] + f[2])); |
| 228 | +} |
| 229 | +} |
| 230 | +return dp[K+1][dst] >= MAX ? -1 : dp[K+1][dst]; |
| 231 | +} |
| 232 | + |
| 233 | + |
| 234 | +public int findCheapestPrice7(int n, int[][] flights, int src, int dst, int K) { |
| 235 | +int[][] dp = new int[2][n]; |
| 236 | +Arrays.fill(dp[0], MAX); |
| 237 | +Arrays.fill(dp[1], MAX); |
| 238 | +dp[0][src] = 0; |
| 239 | +int k = 1; |
| 240 | +while (k <= K+1) { |
| 241 | +for (int[] f: flights) { |
| 242 | +dp[k%2][f[1]] = Math.min(dp[k%2][f[1]], Math.min(dp[(k-1)%2][f[1]], |
| 243 | +dp[(k-1)%2][f[0]] + f[2])); |
| 244 | +} |
| 245 | +k++; |
| 246 | +} |
| 247 | +k--; |
| 248 | +return dp[k%2][dst] >= MAX ? -1 : dp[k%2][dst]; |
| 249 | +} |
| 250 | + |
134 | 251 | }
|
135 | 252 |
|
0 commit comments