QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓

#1445. Shortest Path 1

Statistics

Given a weighted directed graph with $n$ vertices and $m$ edges, find the shortest path from the starting vertex $s$ to all other vertices.

Input

The first line contains three space-separated integers $n$, $m$, and $s$.

The following $m$ lines each contain three positive integers $s_i$, $t_i$, and $w_i$ ($1 \le s_i, t_i \le n$, $0 \leq w_i \leq 10 ^ 9$), representing a directed edge from $s_i$ to $t_i$ with weight $w_i$. The graph may contain multiple edges between the same pair of vertices or self-loops.

Output

Output a single line containing $n$ integers $d_1, d_2, \ldots, d_n$, where $d_i$ is the length of the shortest path from $s$ to $i$. If no such path exists, output $-1$.

Examples

Input 1

4 5 1
1 2 1000
2 3 10000
3 1 1
3 4 100000
3 4 1000000

Output 1

0 1000 11000 111000

Input 2

2 1 2
1 2 1

Output 2

-1 0

Subtasks

For all test cases, $1 \le n \le 3 \cdot 10^5$ and $0 \le m \le 10^6$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.