Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
- int[][] g = new int[n][n];
- for(int[] flight : flights){
- int source = flight[0], target = flight[1], cost = flight[2];
- g[source][target] = cost;
- }
- PriorityQueue<int[]> heap = new PriorityQueue<>(new Comparator<int[]>(){
- public int compare(int[] o1, int[] o2){
- return Integer.compare(o1[2], o2[2]);
- }
- });
- //int array with node, stops, price.
- heap.offer(new int[]{src, K+1, 0});
- while(!heap.isEmpty()){
- int[] curr = heap.poll();
- int vert = curr[0], remStops = curr[1], price = curr[2];
- if(vert == dst) return price;
- if(remStops > 0){
- for(int i = 0; i<n; i++){
- if(g[vert][i] > 0){
- heap.offer(new int[]{i, remStops - 1, price+g[vert][i]});
- }
- }
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement