LeetCode Solutions

743. Network Delay Time

Time: $O(|E| + |V|\log |E|)$

Space: $O(|V|)$

			

class Solution {
 public:
  int networkDelayTime(vector<vector<int>>& times, int n, int k) {
    using P = pair<int, int>;
    vector<vector<P>> graph(n);
    priority_queue<P, vector<P>, greater<>> minHeap;  // (d, u)
    vector<bool> seen(n);

    for (const vector<int>& t : times) {
      const int u = t[0] - 1;
      const int v = t[1] - 1;
      const int w = t[2];
      graph[u].emplace_back(v, w);
    }

    minHeap.emplace(0, k - 1);

    while (!minHeap.empty()) {
      const auto [d, u] = minHeap.top();
      minHeap.pop();
      if (seen[u])
        continue;
      seen[u] = true;
      if (--n == 0)
        return d;
      for (const auto& [v, w] : graph[u])
        minHeap.emplace(d + w, v);
    }

    return -1;
  }
};
			

class Solution {
  public int networkDelayTime(int[][] times, int n, int k) {
    List<Pair<Integer, Integer>>[] graph = new List[n];
    Queue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); // (d, u)
    boolean[] seen = new boolean[n];

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] t : times) {
      final int u = t[0] - 1;
      final int v = t[1] - 1;
      final int w = t[2];
      graph[u].add(new Pair<>(v, w));
    }

    minHeap.offer(new int[] {0, k - 1});

    while (!minHeap.isEmpty()) {
      final int d = minHeap.peek()[0];
      final int u = minHeap.poll()[1];
      if (seen[u])
        continue;
      seen[u] = true;
      if (--n == 0)
        return d;
      for (Pair<Integer, Integer> node : graph[u]) {
        final int v = node.getKey();
        final int w = node.getValue();
        minHeap.offer(new int[] {d + w, v});
      }
    }

    return -1;
  }
}