787. Cheapest Flights Within K Stops
作者:互联网
1 import java.util.*; 2 3 public class CheapestFlightsWithinKStops { 4 /** 5 * 787. Cheapest Flights Within K Stops 6 * Medium 7 * <p> 8 * There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w. 9 * 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. 10 * Example 1: 11 * Input: 12 * n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]] 13 * src = 0, dst = 2, k = 1 14 * Output: 200 15 * Explanation: 16 * The graph looks like this: 17 * The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture. 18 * Example 2: 19 * Input: 20 * n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]] 21 * src = 0, dst = 2, k = 0 22 * Output: 500 23 * Explanation: 24 * The graph looks like this: 25 * The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture. 26 * Note: 27 * The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1. 28 * The size of flights will be in range [0, n * (n - 1) / 2]. 29 * The format of each flight will be (src, dst, price). 30 * The price of each flight will be in the range [1, 10000]. 31 * k is in the range of [0, n - 1]. 32 * There will not be any duplicated flights or self cycles. 33 */ 34 public int solution1(int n, int[][] flights, int src, int dst, int k) { 35 Map<Integer, Map<Integer, Integer>> prices = new HashMap<>(); 36 for (int[] f : flights) { 37 if (!prices.containsKey(f[0])) prices.put(f[0], new HashMap<>()); 38 prices.get(f[0]).put(f[1], f[2]); 39 } 40 Queue<int[]> pq = new PriorityQueue<>((a, b) -> (Integer.compare(a[0], b[0]))); 41 pq.add(new int[]{0, src, k + 1}); 42 while (!pq.isEmpty()) { 43 int[] top = pq.remove(); 44 int price = top[0]; 45 int city = top[1]; 46 int stops = top[2]; 47 if (city == dst) return price; 48 if (stops > 0) { 49 Map<Integer, Integer> adj = prices.getOrDefault(city, new HashMap<>()); 50 for (int a : adj.keySet()) { 51 pq.add(new int[]{price + adj.get(a), a, stops - 1}); 52 } 53 } 54 } 55 return -1; 56 } 57 58 public int solution2(int n, int[][] flights, int src, int dst, int K) { 59 int[][] srcToDst = new int[n][n]; 60 for (int i = 0; i < flights.length; i++) 61 srcToDst[flights[i][0]][flights[i][1]] = flights[i][2]; 62 63 PriorityQueue<City> minHeap = new PriorityQueue(); 64 minHeap.offer(new City(src, 0, 0)); 65 66 int[] cost = new int[n]; 67 Arrays.fill(cost, Integer.MAX_VALUE); 68 cost[src] = 0; 69 int[] stop = new int[n]; 70 Arrays.fill(stop, Integer.MAX_VALUE); 71 stop[src] = 0; 72 73 while (!minHeap.isEmpty()) { 74 City curCity = minHeap.poll(); 75 if (curCity.id == dst) return curCity.costFromSrc; 76 if (curCity.stopFromSrc == K + 1) continue; 77 int[] nexts = srcToDst[curCity.id]; 78 for (int i = 0; i < n; i++) { 79 if (nexts[i] != 0) { 80 int newCost = curCity.costFromSrc + nexts[i]; 81 int newStop = curCity.stopFromSrc + 1; 82 if (newCost < cost[i]) { 83 minHeap.offer(new City(i, newCost, newStop)); 84 cost[i] = newCost; 85 } else if (newStop < stop[i]) { 86 minHeap.offer(new City(i, newCost, newStop)); 87 stop[i] = newStop; 88 } 89 } 90 } 91 } 92 return cost[dst] == Integer.MAX_VALUE ? -1 : cost[dst]; 93 } 94 95 private class City implements Comparable<City> { 96 int id; 97 int costFromSrc; 98 int stopFromSrc; 99 100 public City(int id, int costFromSrc, int stopFromSrc) { 101 this.id = id; 102 this.costFromSrc = costFromSrc; 103 this.stopFromSrc = stopFromSrc; 104 } 105 106 public boolean equals(City c) { 107 if (c instanceof City) 108 return this.id == c.id; 109 return false; 110 } 111 112 public int compareTo(City c) { 113 return this.costFromSrc - c.costFromSrc; 114 } 115 } 116 }
标签:src,787,city,int,dst,Within,flights,Flights,new 来源: https://www.cnblogs.com/bangpenggao/p/11328735.html