cswork / OAOpen practice workspace

WaymoOA #2

Shortest Paths to Multiple Targets

Estimated MediumJudge-ready

Topics (AI-labelled)Shortest Paths

On a directed graph with non-negative weights, find the shortest paths from source to multiple targets; unreachable targets are -1.

See "Input" for the standard input/output conventions and constraints added by this site.

Input

n≤200000, m≤300000, w≤10^9; nodes are 1-based, and the input is n m source q, then the edges, then the targets.

Output

Output the target distances in order.

Example 1

Input:

5 6 1 3
1 2 2
1 3 5
2 3 1
2 4 2
3 5 1
4 5 2
3 4 5

Output:

3 4 4

Example 2

Input:

4 1 1 2
1 2 5
2 3

Output:

5 -1

Example 3

Input:

3 2 2 2
2 1 0
1 3 0
1 3

Output:

0 0

Editorial

Loading solution…