Problem: Find Edges in Shortest Paths

You are given an undirected weighted graph of `n` nodes numbered from 0 to `n
- 1`. The graph consists of `m` edges represented by a 2D array `edges`, where
`edges[i] = [ai, bi, wi]` indicates that there is an edge between nodes `ai`
and `bi` with weight `wi`.

Consider all the shortest paths from node 0 to node `n - 1` in the graph. You
need to find a boolean array `answer` where `answer[i]` is `true` if the edge
`edges[i]` is part of at least one shortest path. Otherwise, `answer[i]` is
`false`.

Return the array `answer`.

Note that the graph may not be connected.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def findAnswer(self, n: int, edges: List[List[int]]) -> List[bool]:
    graph = [[] for _ in range(n)]

    for u, v, w in edges:
      graph[u].append((v, w))
      graph[v].append((u, w))

    from0 = self._dijkstra(graph, 0)
    from1 = self._dijkstra(graph, n - 1)
    return [from0[u] + w + from1[v] == from0[-1] or from0[v] + w + from1[u] == from0[-1] for u, v, w in edges]

  def _dijkstra(self, graph: List[List[Tuple[int, int]]], src: int) -> List[int]:
    dist = [10**9] * len(graph)

    dist[src] = 0
    minHeap = [(dist[src], src)]

    while minHeap:
      d, u = heapq.heappop(minHeap)
      if d > dist[u]:
        continue
      for v, w in graph[u]:
        if d + w < dist[v]:
          dist[v] = d + w
          heapq.heappush(minHeap, (dist[v], v))

    return dist


Java solution:
class Solution {
  // Similar to 2203. Minimum Weighted Subgraph With the Required Paths
  public boolean[] findAnswer(int n, int[][] edges) {
    boolean[] ans = new boolean[edges.length];
    List<Pair<Integer, Integer>>[] graph = new List[n];
    Arrays.setAll(graph, i -> new ArrayList<>());

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

    int[] from0 = dijkstra(graph, 0);
    int[] from1 = dijkstra(graph, n - 1);

    for (int i = 0; i < edges.length; ++i) {
      final int u = edges[i][0];
      final int v = edges[i][1];
      final int w = edges[i][2];
      ans[i] = from0[u] + w + from1[v] == from0[n - 1] || //
               from0[v] + w + from1[u] == from0[n - 1];
    }

    return ans;
  }

  private static int MAX = 1_000_000_000;

  private int[] dijkstra(List<Pair<Integer, Integer>>[] graph, int src) {
    int[] dist = new int[graph.length];
    Arrays.fill(dist, MAX);

    dist[src] = 0;
    Queue<Pair<Integer, Integer>> minHeap =
        new PriorityQueue<>(Comparator.comparingInt(Pair::getKey)) {
          { offer(new Pair<>(dist[src], src)); } // (d, u)
        };

    while (!minHeap.isEmpty()) {
      final int d = minHeap.peek().getKey();
      final int u = minHeap.poll().getValue();
      if (d > dist[u])
        continue;
      for (Pair<Integer, Integer> pair : graph[u]) {
        final int v = pair.getKey();
        final int w = pair.getValue();
        if (d + w < dist[v]) {
          dist[v] = d + w;
          minHeap.offer(new Pair<>(dist[v], v));
        }
      }
    }

    return dist;
  }
};


C++ solution:
class Solution {
 public:
  // Similar to 2203. Minimum Weighted Subgraph With the Required Paths
  vector<bool> findAnswer(int n, vector<vector<int>>& edges) {
    vector<bool> ans;
    vector<vector<pair<int, int>>> graph(n);

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

    const vector<int> from0 = dijkstra(graph, 0);
    const vector<int> from1 = dijkstra(graph, n - 1);

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      const int w = edge[2];
      ans.push_back(from0[u] + w + from1[v] == from0[n - 1] ||
                    from0[v] + w + from1[u] == from0[n - 1]);
    }

    return ans;
  }

 private:
  static constexpr int kMax = 1'000'000'000;

  vector<int> dijkstra(const vector<vector<pair<int, int>>>& graph, int src) {
    vector<int> dist(graph.size(), kMax);

    dist[src] = 0;
    using P = pair<int, int>;  // (d, u)
    priority_queue<P, vector<P>, greater<>> minHeap;
    minHeap.emplace(dist[src], src);

    while (!minHeap.empty()) {
      const auto [d, u] = minHeap.top();
      minHeap.pop();
      if (d > dist[u])
        continue;
      for (const auto& [v, w] : graph[u])
        if (d + w < dist[v]) {
          dist[v] = d + w;
          minHeap.emplace(dist[v], v);
        }
    }

    return dist;
  }
};