You are given an undirected weighted graph of n nodes numbered from 0 to n - 1. The graph has m edges given in a 2D array edges, where edges[i] = [a_i, b_i, w_i] means there is an edge between nodes a_i and b_i with weight w_i.
Consider all the shortest paths from node 0 to node n - 1. Return a boolean array answer where answer[i] is True if the edge edges[i] is part of at least one shortest path, and False otherwise. Note that the graph may not be connected.
Function Signature
def find_shortest_path_edges(n: int, edges: list[list[int]]) -> list[bool]:
Rules
-
Edges are undirected, so a path may cross
edges[i]
from
a_i
to
b_i
or from
b_i
to
a_i
.
-
answer
has the same length as
edges
, and
answer[i]
refers to
edges[i]
.
-
When several paths tie for the shortest distance, an edge counts if it lies on any one of them.
-
If node
n - 1
cannot be reached from node
0
, every entry of
answer
is
False
.
Constraints
-
2 <= n <= 5 * 10^4
-
m == len(edges)
-
1 <= m <= min(5 * 10^4, n * (n - 1) / 2)
-
0 <= a_i, b_i < n
-
a_i != b_i
-
1 <= w_i <= 10^5
-
There are no repeated edges.
Examples
Input: n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]
Output: [True,True,True,False,True,True,True,False]
The shortest distance from node 0 to node 5 is 5, and three paths reach it: 0 -> 1 -> 5, 0 -> 2 -> 3 -> 5 and 0 -> 2 -> 3 -> 1 -> 5. Together they use every edge except [1,4,3] and [4,5,2], and the cheapest path through either of those two edges has length 9.
Input: n = 4, edges = [[2,0,1],[0,1,1],[0,3,4],[3,2,2]]
Output: [True,False,False,True]
The only shortest path is 0 -> 2 -> 3 with length 3, which uses edges [2,0,1] and [3,2,2]. The direct edge [0,3,4] costs 4, and edge [0,1,1] leads to node 1, which has no other edge.
Input: n = 3, edges = [[0,1,2]]
Output: [False]
Node 2 has no edges, so no path from node 0 to node 2 exists and every entry is False.