#P3371. 【模板】单源最短路径(弱化版)

【模板】单源最短路径(弱化版)

Description

As stated, given a directed graph, output the shortest path length from a specified vertex to every vertex.

Input Format

The first line contains three integers n,m,sn, m, s, denoting the number of vertices, the number of directed edges, and the index of the source vertex.

Each of the next mm lines contains three integers u,v,wu, v, w, denoting a directed edge uvu \to v with length ww.

Output Format

Output nn integers on one line. The ii-th integer is the shortest path distance from ss to vertex ii. If a vertex is unreachable, output 23112^{31} - 1.

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

Hint

  • Constraints
    For 20%20\% of the testdata: 1n51 \le n \le 5, 1m151 \le m \le 15;
    For 40%40\% of the testdata: 1n1001 \le n \le 100, 1m1041 \le m \le 10^4;
    For 70%70\% of the testdata: 1n10001 \le n \le 1000, 1m1051 \le m \le 10^5;
    For 100%100\% of the testdata: 1n1041 \le n \le 10^4, 1m5×1051 \le m \le 5 \times 10^5, 1u,vn1 \le u, v \le n, w0w \ge 0, w<231\sum w < 2^{31}; the testdata is guaranteed to be random.

  • Update 2022/07/29: There may be multiple edges between two vertices. Please take note.

  • For the full 100%100\% testdata, please refer to P4779. Note that its constraints differ slightly from this problem.

Sample note:

In the image, the text positions for "1 to 3" and "1 to 4" are swapped.

Translated by ChatGPT 5