Find the Closest Marked Node Problem
Description
LeetCode Problem 2737.
You are given a positive integer n which is the number of nodes of a 0-indexed directed weighted graph and a 0-indexed 2D array edges where edges[i] = [u_i, v_i, w_i] indicates that there is an edge from node u_i to node v_i with weight w_i.
You are also given a node s and a node array marked; your task is to find the minimum distance from s to any of the nodes in marked.
Return an integer denoting the minimum distance from s to any node in marked or -1 if there are no paths from s to any of the marked nodes.
Example 1:
Input: n = 4, edges = [[0,1,1],[1,2,3],[2,3,2],[0,3,4]], s = 0, marked = [2,3]
Output: 4
Explanation: 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.
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.
The minimum of them is 4.

Example 2:
Input: n = 5, edges = [[0,1,2],[0,2,4],[1,3,1],[2,3,3],[3,4,2]], s = 1, marked = [0,4]
Output: 3
Explanation: There are no paths from node 1 (the green node) to node 0 (a red node).
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.
So the answer is 3.

Example 3:
Input: n = 4, edges = [[0,1,1],[1,2,3],[2,3,2]], s = 3, marked = [0,1]
Output: -1
Explanation: There are no paths from node 3 (the green node) to any of the marked nodes (the red nodes), so the answer is -1.

Constraints:
2 <= n <= 5001 <= edges.length <= 10^4edges[i].length = 30 <= edges[i][0], edges[i][1] <= n - 11 <= edges[i][2] <= 10^61 <= marked.length <= n - 10 <= s, marked[i] <= n - 1s != marked[i]marked[i] != marked[j]for everyi != j- The graph might have repeated edges.
- The graph is generated such that it has no self-loops.
Solution
Approach: Dijkstra's Algorithm
Run a single-source shortest path computation from s, then take the smallest distance among the marked nodes.
With n ≤ 500, an adjacency matrix is convenient: g[u][v] holds the smallest weight among the (possibly repeated) edges from u to v, or infinity if there is none. Then run Dijkstra's algorithm without a heap: repeat n times, pick the unvisited node t with the smallest tentative distance, mark it visited, and relax dist[j] = min(dist[j], dist[t] + g[t][j]) for every node j.
Finally return the minimum of dist[m] over the marked nodes m, or -1 if that minimum is still infinity.
Complexity: O(n2) time and O(n2) space.
from math import inf
class Solution:
def minimumDistance(
self, n: int, edges: List[List[int]], s: int, marked: List[int]
) -> int:
g = [[inf] * n for _ in range(n)]
for u, v, w in edges:
g[u][v] = min(g[u][v], w)
dist = [inf] * n
vis = [False] * n
dist[s] = 0
for _ in range(n):
t = -1
for j in range(n):
if not vis[j] and (t == -1 or dist[t] > dist[j]):
t = j
vis[t] = True
for j in range(n):
dist[j] = min(dist[j], dist[t] + g[t][j])
ans = min(dist[i] for i in marked)
return -1 if ans >= inf else ans