You are given a positive integer n which is the number of nodes of a
0-indexed directed weighted graph and a 0-indexed2D arrayedges
where edges[i] = [ui, vi, wi] indicates that there is an edge from node ui
to node vi with weight wi.
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 fromsto any node inmarkedor-1if there are no paths from s to any of the marked nodes.
Input: n =4, edges =[[0,1,1],[1,2,3],[2,3,2],[0,3,4]], s =0, marked =[2,3]Output: 4Explanation: There is one path from node 0(the green node) to node 2(a red node), which is0->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 is4.
Example 2:
1
2
3
4
5
6
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: 3Explanation: 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 is1->3->4, and has a distance of 1+2=3.So the answer is3.
Example 3:
1
2
3
4
Input: n =4, edges =[[0,1,1],[1,2,3],[2,3,2]], s =3, marked =[0,1]Output: -1Explanation: 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 <= 500
1 <= edges.length <= 10^4
edges[i].length = 3
0 <= edges[i][0], edges[i][1] <= n - 1
1 <= edges[i][2] <= 10^6
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.
We need the shortest path from the source node s to any node in the marked set. Dijkstra’s algorithm is ideal for finding the shortest path in a weighted directed graph with non-negative weights.
classSolution {
publicintfindClosestMarkedNode(int n, int[][] edges, int s, int[] marked) {
List<int[]>[] g =new List[n];
for (int i = 0; i < n; ++i) g[i]=new ArrayList<>();
for (int[] e : edges) g[e[0]].add(newint[]{e[1], e[2]});
long[] dist =newlong[n];
Arrays.fill(dist, Long.MAX_VALUE);
PriorityQueue<long[]> pq =new PriorityQueue<>(Comparator.comparingLong(a -> a[0]));
dist[s]= 0;
pq.offer(newlong[]{0, s});
while (!pq.isEmpty()) {
long[] top = pq.poll();
long d = top[0]; int u = (int)top[1];
if (d > dist[u]) continue;
for (int[] e : g[u]) {
int v = e[0], w = e[1];
if (dist[v]> d + w) {
dist[v]= d + w;
pq.offer(newlong[]{dist[v], v});
}
}
}
long ans = Long.MAX_VALUE;
for (int v : marked) ans = Math.min(ans, dist[v]);
return ans == Long.MAX_VALUE?-1 : (int)ans;
}
}
classSolution {
funfindClosestMarkedNode(n: Int, edges: Array<IntArray>, s: Int, marked: IntArray): Int {
val g = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (e in edges) g[e[0]].add(e[1] to e[2])
val dist = LongArray(n) { Long.MAX_VALUE }
val pq = java.util.PriorityQueue(compareBy<Pair<Long, Int>> { it.first })
dist[s] = 0L pq.add(0L to s)
while (pq.isNotEmpty()) {
val(d, u) = pq.poll()
if (d > dist[u]) continuefor ((v, w) in g[u]) {
if (dist[v] > d + w) {
dist[v] = d + w
pq.add(dist[v] to v)
}
}
}
var ans = Long.MAX_VALUE
for (v in marked) ans = minOf(ans, dist[v])
returnif (ans ==Long.MAX_VALUE) -1else ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import heapq
classSolution:
deffindClosestMarkedNode(self, n: int, edges: list[list[int]], s: int, marked: list[int]) -> int:
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
dist = [float('inf')] * n
dist[s] =0 h = [(0, s)]
while h:
d, u = heapq.heappop(h)
if d > dist[u]: continuefor v, w in g[u]:
if dist[v] > d + w:
dist[v] = d + w
heapq.heappush(h, (dist[v], v))
ans = min((dist[v] for v in marked), default=float('inf'))
return-1if ans == float('inf') else ans