Shortest Paths to Multiple Targets
You are given a directed weighted graph with n nodes labeled 1 through n. Each edge is represented as [u, v, w], meaning there is an edge from u to v with non-negative weight w.
Also given are a source node source and a list of target nodes targets. Compute the shortest distance from source to each target in order. If a target is unreachable, return -1 for that target.
Complete the function shortestPathsToTargets in the editor below.
shortestPathsToTargets has the following parameters:
int n: the number of nodesint[][] edges: directed edges[u, v, w]int source: the source nodeint[] targets: the targets to query
Returns
long[]: shortest distances to the targets in the same order.
1Example 1
Running Dijkstra from node 1 gives distances 3, 4, and 4 to targets 3, 4, and 5.
2Example 2
Node 2 is reachable with cost 5, while node 3 is unreachable.
Constraints
Limits and guarantees your solution can rely on.
1 <= n <= 2 * 10^50 <= edges.length <= 3 * 10^50 <= w <= 10^9- Multiple edges and self-loops are allowed.