PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000222. Đường đi ngắn nhất I (Shortest Routes I)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi ngắn nhất I (Shortest Routes I)

Shortest Routes I

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

You have nn cities and mm directed flights. City 11 is the starting point. A flight from aa to bb has positive length cc. Multiple flights between the same endpoints may exist.

Determine the shortest distance from city 11 to every city. All cities are guaranteed to be reachable from city 11.

Input

  • The first line contains n,mn,m.
  • Each of the next mm lines contains a,b,ca,b,c, a directed flight from aa to bb of length cc. Cities are numbered 11 through nn.

Output

Print nn integers in city order 1,2,…,n1,2,\ldots,n: the shortest distances from city 11.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤m≤1001\le m\le 100; 1≤a,b≤n1\le a,b\le n; 1≤c≤1091\le c\le 10^9; mọi đỉnh đến được từ đỉnh 1; cho phép cạnh trùng và khuyên.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤100001\le m\le 10000; 1≤a,b≤n1\le a,b\le n; 1≤c≤1091\le c\le 10^9; mọi đỉnh đến được từ đỉnh 1; cho phép cạnh trùng và khuyên.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤a,b≤n1\le a,b\le n; 1≤c≤1091\le c\le 10^9; mọi đỉnh đến được từ đỉnh 1; cho phép cạnh trùng và khuyên.

Examples

Input

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

Output

0 5 2

Explanation

To reach city 11 costs 00. To city 33, use the direct flight of length 22 rather than the one of length 44. To city 22, take 1→3→21\to3\to2 of length 2+3=52+3=5, shorter than the direct length 66. Thus the output is 0 5 2.