Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. package algorithms.leetcode;
  2.  
  3. import java.util.*;
  4.  
  5. public class Solution {
  6.  
  7.     public static void main(String[] args) {
  8.         /*int[][] graph = {
  9.                 {2,1,1},
  10.                 {2,3,1},
  11.                 {3,4,1}
  12.         };
  13.         int N = 4;
  14.         int K = 2;*/
  15.  
  16.         /*int[][] graph = {{2,1,1},{2,3,1},{3,4,1}};
  17.         int N = 4;
  18.         int K = 2;*/
  19.  
  20.         /*int[][] graph = {{1,2,1}};
  21.         int N = 2;
  22.         int K = 1;*/
  23.  
  24.         /*int[][] graph = {{1,2,1},{2,1,3}};
  25.         int N = 2;
  26.         int K = 2;*/
  27.  
  28.         /*int[][] graph = {{1,2,1},{2,3,2}};
  29.         int N = 3;
  30.         int K = 1;*/
  31.  
  32.         /*int[][] graph = {{1,2,1},{2,1,3}};
  33.         int N = 2;
  34.         int K = 2;*/
  35.  
  36.         int[][] graph = {{1,2,1},{2,3,2},{1,3,2}};
  37.         int N = 3;
  38.         int K = 1;
  39.        
  40.         /*
  41.         trono en este caso
  42.         [[2,4,10],[5,2,38],[3,4,33],[4,2,76],[3,2,64],[1,5,54],[1,4,98],[2,3,61],[2,1,0],[3,5,77],[5,1,34],[3,1,79],[5,3,2],[1,2,59],[4,3,46],[5,4,44],[2,5,89],[4,5,21],[1,3,86],[4,1,95]]
  43.         5
  44.         1
  45.         * */
  46.  
  47.         Solution.networkDelayTime(graph, N, K);
  48.     }
  49.  
  50.     public static int networkDelayTime(int[][] times, int N, int K) {
  51.         if (times.length == 0) return - 1;
  52.  
  53.         Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
  54.         for (int[] time : times) {
  55.             if (!graph.containsKey(time[0])) {
  56.                 graph.put(time[0], new HashMap<>());
  57.             }
  58.             graph.get(time[0]).put(time[1], time[2]);
  59.         }
  60.  
  61.         if (!graph.containsKey(K)) return -1;
  62.  
  63.         Map<Integer, Integer> visited = new HashMap<>();
  64.         boolean[] alreadyVisited = new boolean[N + 1];
  65.         PriorityQueue<Integer> maxResult = new PriorityQueue<>(N, Collections.reverseOrder());
  66.         depthFirstSearch(graph, K, 0, visited, alreadyVisited);
  67.  
  68.         if (visited.size() != N) return -1;
  69.  
  70.         for (Map.Entry<Integer, Integer> iterateOverVisited : visited.entrySet()) {
  71.             maxResult.add(iterateOverVisited.getValue());
  72.         }
  73.         return maxResult.peek();
  74.     }
  75.  
  76.     public static void depthFirstSearch(Map<Integer, Map<Integer, Integer>> graph, int currVal, int currTime, Map<Integer, Integer> visited, boolean[] alreadyVisited) {
  77.         if (!alreadyVisited[currVal])
  78.             visited.put(currVal, currTime);
  79.         else
  80.             visited.put(currVal, Math.min(currTime, visited.get(currVal)));
  81.         if (graph.containsKey(currVal) && !alreadyVisited[currVal]) {
  82.             for (Map.Entry<Integer, Integer> neighboors : graph.get(currVal).entrySet()) {
  83.                 alreadyVisited[currVal] = true;
  84.                 depthFirstSearch(graph, neighboors.getKey(), neighboors.getValue() + currTime, visited, alreadyVisited);
  85.             }
  86.         } else {
  87.             alreadyVisited[currVal] = true;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement