Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. class Solution {
  2.  
  3. private static class PQKey {
  4. double price;
  5. int stopsFromSource;
  6. public PQKey(double price, int stopsFromSource) {
  7. this.price = price;
  8. this.stopsFromSource = stopsFromSource;
  9. }
  10. }
  11.  
  12. private static class IndexMinPQ {
  13.  
  14. private int maxSize;
  15. private int[] pq; //holds index at each size of pq;
  16. private int[] qp; //the inverse of pq, holds the size at each index of the input
  17. private PQKey[] keys; //these are the values by which we are going to sort PQ
  18. private int n; //current size of the pq
  19.  
  20. public IndexMinPQ(int capacity) {
  21. this.maxSize = capacity;
  22. this.keys = new PQKey[maxSize];
  23. this.pq = new int[maxSize+1]; // +1 because the index is size
  24. this.qp = new int[maxSize];
  25.  
  26. for(int i=0; i < maxSize; i++)
  27. qp[i] = -1;
  28.  
  29. }
  30.  
  31. public boolean containsKey(int k) {
  32. return qp[k] != -1;
  33. }
  34.  
  35. public boolean isEmpty() {
  36. return n == 0;
  37. }
  38.  
  39.  
  40. public void insert(int i, PQKey key) {
  41. n++;
  42. this.qp[i] = n;
  43. this.pq[n] = i;
  44. this.keys[i] = key;
  45. //System.out.println("i --> " + i + ", n --> " + n + ", i --> " + i);
  46. swim(n);
  47. }
  48.  
  49.  
  50. public int[] deleteMin() {
  51. int[] result = new int[2];
  52.  
  53. int min = pq[1];
  54. result[0] = min;
  55.  
  56. exch(1,n--);
  57. sink(1);
  58. qp[min] = -1;
  59.  
  60. result[1] = keys[min].stopsFromSource;
  61.  
  62. keys[min] = null;
  63.  
  64. return result;
  65. }
  66.  
  67. public int[] peek() {
  68. int[] result = new int[2];
  69.  
  70. int min = pq[1];
  71. result[0] = min;
  72. result[1] = keys[min].stopsFromSource;
  73.  
  74. return result;
  75. }
  76.  
  77.  
  78. public void decreaseKey(int i, PQKey key) {
  79. this.keys[i] = key;
  80. swim(qp[i]);
  81. }
  82.  
  83. private void swim(int k) {
  84.  
  85. while(k > 1 && greater(k/2,k)) {
  86. exch(k/2,k);
  87. k = k/2;
  88. }
  89.  
  90. }
  91.  
  92.  
  93. private void sink(int k) {
  94. while(2*k <= n) {
  95. int j = 2*k;
  96. if(j < n && greater(j,j+1)) j++;
  97. if(!greater(k,j)) break;
  98. exch(k,j);
  99. k = j; //this is very important, it is not k = 2*k;
  100. }
  101. }
  102.  
  103. private void exch(int i, int j) {
  104.  
  105. //i and j are sizes
  106. //since pq holds holds the size as index
  107. //exchange pq first
  108.  
  109. int swap = pq[i];
  110. pq[i] = pq[j];
  111. pq[j] = swap;
  112.  
  113. //now exchange corresponding indexes in qp array
  114.  
  115. qp[pq[i]] = i; //notice i --> i correspondence, its because pq[i] has the correct index that needs to change is'v alue
  116. qp[pq[j]] = j; //notice j --> j correspondence
  117.  
  118.  
  119.  
  120. }
  121.  
  122. private boolean greater(int i, int j) {
  123. //System.out.println("i --> " + i + ", j --> " + j + ", n --> " + n);
  124.  
  125. return keys[pq[i]].price-keys[pq[j]].price > 0 ? true: false;
  126. }
  127.  
  128. }
  129.  
  130. public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
  131.  
  132.  
  133. int cheapestPrice = Integer.MAX_VALUE;
  134.  
  135. //construct digraph
  136.  
  137. Map<Integer,List<int[]>> digraph = new HashMap<>();
  138. int m = flights.length;
  139. for(int i=0; i < m; i++) {
  140.  
  141. int from = flights[i][0];
  142. if(!digraph.containsKey(from)) {
  143. digraph.put(from, new ArrayList<>());
  144. }
  145.  
  146. List<int[]> adjList = digraph.get(from);
  147. adjList.add(flights[i]);
  148. }
  149.  
  150.  
  151. int[][] edgeTo = new int[n][3];
  152. double[] distTo = new double[n];
  153.  
  154. IndexMinPQ pq = new IndexMinPQ(n);
  155.  
  156. for(int i=0; i < n; i++) {
  157. distTo[i] = Double.POSITIVE_INFINITY;
  158. }
  159. distTo[src] = 0;
  160. pq.insert(src, new PQKey(0,0));
  161.  
  162. while(!pq.isEmpty()) {
  163. int[] minNode = pq.deleteMin();
  164.  
  165. int from = minNode[0];
  166. int stopsFromSource = minNode[1];
  167.  
  168. //System.out.println("from --> " + from + ", stopsFromSource --> " + stopsFromSource);
  169.  
  170. /*if(stopsFromSource > K){
  171. continue;
  172. }*/
  173.  
  174. if(from == dst) {
  175. return (int)distTo[dst];
  176. }
  177. if(stopsFromSource <= K) {
  178. if(digraph.containsKey(from)) {
  179.  
  180. List<int[]> adjList = digraph.get(from);
  181. for(int[] edge: adjList) {
  182. int v = edge[0];
  183. assert v == from;
  184. int w = edge[1];
  185. System.out.println("v --> " + v + ", w --> " + w);
  186.  
  187. //System.out.println("distTo[w] --> " + distTo[w] + ", distTo[v] --> " + distTo[v] + ", edge[2] --> " + edge[2] );
  188.  
  189.  
  190. if(distTo[w] > distTo[v] + edge[2]) {
  191. distTo[w] = distTo[v] + edge[2];
  192. edgeTo[w] = edge;
  193.  
  194. System.out.println("distTo[w] --> " + distTo[w]);
  195.  
  196. if(stopsFromSource < K || (dst == w && stopsFromSource == K)) {
  197.  
  198.  
  199. if(pq.containsKey(w)) {
  200. pq.decreaseKey(w, new PQKey(distTo[w],stopsFromSource+1));
  201. }
  202. else {
  203. pq.insert(w, new PQKey(distTo[w],stopsFromSource+1));
  204. }
  205. }
  206.  
  207.  
  208.  
  209. }
  210.  
  211. }
  212. }
  213. }
  214. }
  215.  
  216. return -1;
  217.  
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement