Skip to content
UPEEC

Shortest Cable Lengths

Hard#graphs#dijkstra#shortest-path· solved by 0 · time limit 2.0 s

Junctions 1…n1 \dots n are joined by mm cables (undirected) with positive lengths. Print the shortest distance from junction ss to every junction (−1-1 if unreachable). Use Dijkstra's algorithm with a priority queue.

Input

nn and mm, mm lines u v length, then ss.

Output

nn distances on one line.

Constraints

1≤n≤1051 \le n \le 10^5, 0≤m≤2×1050 \le m \le 2 \times 10^5, 1≤1 \le length ≤104\le 10^4 — use 64-bit distances.

Sample 1

Input

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

Output

0 3 1 8

Sign in or join free to run and submit your solution.