|
| 1 | +<p>You are given a positive integer <code>n</code> which is the number of nodes of a <strong>0-indexed directed weighted</strong> graph and a <strong>0-indexed</strong> <strong>2D array</strong> <code>edges</code> where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p> |
| 2 | + |
| 3 | +<p>You are also given a node <code>s</code> and a node array <code>marked</code>; your task is to find the <strong>minimum</strong> distance from <code>s</code> to <strong>any</strong> of the nodes in <code>marked</code>.</p> |
| 4 | + |
| 5 | +<p>Return <em>an integer denoting the minimum distance from </em><code>s</code><em> to any node in </em><code>marked</code><em> or </em><code>-1</code><em> if there are no paths from s to any of the marked nodes</em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> n = 4, edges = [[0,1,1],[1,2,3],[2,3,2],[0,3,4]], s = 0, marked = [2,3] |
| 12 | +<strong>Output:</strong> 4 |
| 13 | +<strong>Explanation:</strong> There is one path from node 0 (the green node) to node 2 (a red node), which is 0->1->2, and has a distance of 1 + 3 = 4. |
| 14 | +There are two paths from node 0 to node 3 (a red node), which are 0->1->2->3 and 0->3, the first one has a distance of 1 + 3 + 2 = 6 and the second one has a distance of 4. |
| 15 | +The minimum of them is 4. |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-34-38.png" style="width: 185px; height: 180px;" /></p> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> n = 5, edges = [[0,1,2],[0,2,4],[1,3,1],[2,3,3],[3,4,2]], s = 1, marked = [0,4] |
| 24 | +<strong>Output:</strong> 3 |
| 25 | +<strong>Explanation:</strong> There are no paths from node 1 (the green node) to node 0 (a red node). |
| 26 | +There is one path from node 1 to node 4 (a red node), which is 1->3->4, and has a distance of 1 + 2 = 3. |
| 27 | +So the answer is 3. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-35-13.png" style="width: 300px; height: 285px;" /></p> |
| 31 | + |
| 32 | +<p><strong class="example">Example 3:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> n = 4, edges = [[0,1,1],[1,2,3],[2,3,2]], s = 3, marked = [0,1] |
| 36 | +<strong>Output:</strong> -1 |
| 37 | +<strong>Explanation:</strong> There are no paths from node 3 (the green node) to any of the marked nodes (the red nodes), so the answer is -1. |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-35-47.png" style="width: 420px; height: 80px;" /></p> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>2 <= n <= 500</code></li> |
| 47 | + <li><code>1 <= edges.length <= 10<sup>4</sup></code></li> |
| 48 | + <li><code>edges[i].length = 3</code></li> |
| 49 | + <li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li> |
| 50 | + <li><code>1 <= edges[i][2] <= 10<sup>6</sup></code></li> |
| 51 | + <li><code>1 <= marked.length <= n - 1</code></li> |
| 52 | + <li><code>0 <= s, marked[i] <= n - 1</code></li> |
| 53 | + <li><code>s != marked[i]</code></li> |
| 54 | + <li><code>marked[i] != marked[j]</code> for every <code>i != j</code></li> |
| 55 | + <li>The graph might have <strong>repeated edges</strong>.</li> |
| 56 | + <li>The graph is generated such that it has no <strong>self-loops</strong>.</li> |
| 57 | +</ul> |
0 commit comments