943. Find The Closest Marked Node
The number of nodes in a 0-indexed directed weighted graph, represented by the positive integer n, and the edges of a 0-indexed 2D array, where edges[i] = [ui, vi, wi], show that there is an directed edge from node ui to node vi with weight wi, are provided to you.
A node s and a node array marked are also provided to you; your job is to determine the smallest distance between s and any of the nodes in marked.
If there are no paths from s to any of the marked nodes, return -1; otherwise, return an integer representing the minimum distance between s and any node in marked.
Example 1:
Input : n = 5, s = 0, edges = [ [0, 1, 3], [0, 2, 4], [0, 3, 1], [0, 4, 2] ], marked = [1, 2, 3]
Output : 1
Explanation :
The paths from node s (0) to marked nodes is as below :
0 to 1 -> 3
0 to 2 -> 4
0 to 3 -> 1
So the smallest distance is 1 i.e. from 0 to node 3.
Example 2:
Input : n = 5, s = 1, edges = [ [0, 1, 5], [0, 2, 4], [0, 3, 1], [0, 4, 2], [1, 2, 1], [2, 3, 1] ], marked = [3, 4]
Output : 2
Explanation :
The paths from node s (1) to marked nodes is as below :
1 to 3 -> 1 + 1 -> 2.
1 to 4 -> -1 ( There is no path from 1 to 4).
So the smallest distance is 2 i.e. from 1 to node 3.
Now Your Turn!
Pick the correct output for the given inputInput : n = 5, s = 1, edges = [ [0, 1, 3], [0, 2, 4], [0, 3, 1], [0, 4, 2] ], marked = [2, 3, 4]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= n <= 500
- 1 <= edges.length <= 104
- edges[i].length = 3
- 0 <= edges[i][0], edges[i][1] <= n - 1
- 1 <= edges[i][2] <= 106
- 1 <= marked.length <= n - 1
- 0 <= s, marked[i] <= n - 1
- s != marked[i]
- marked[i] != marked[j] for every i != j
- The graph might have repeated edges.
- The graph is generated such that it has no self-loops.