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

#GD0000237. Đường đi ngắn nhất khi có cạnh âm (Greedy vs Dijkstra - Negative Edge)

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 khi có cạnh âm (Greedy vs Dijkstra - Negative Edge)

Greedy vs Dijkstra - Negative Edge

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a directed graph of nn vertices and mm edges, where edge u→vu\to v has integer weight ww, possibly negative. A path's length is the sum of its edge weights. There are no negative-weight cycles.

Find the shortest distances from vertex 11 to all vertices. Print INF for an unreachable vertex. Parallel edges and nonnegative self-loops are permitted.

This exercise examines the failure of finalizing shortest distances too early when negative edges exist.

Input

  • The first line contains n,mn,m.
  • Each of the next mm lines contains u,v,wu,v,w, a directed weighted edge.

Output

Print nn space-separated values: the shortest distance from 11 to vertex ii, or INF if unreachable.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 0≤m≤50000\le m\le 5000; 1≤u,v≤n1\le u,v\le n; −1000≤w≤1000-1000\le w\le 1000; không có chu trình trọng số âm.
  • Subtask 2 (30 points): 1≤n≤301\le n\le 30; 0≤m≤50000\le m\le 5000; 1≤u,v≤n1\le u,v\le n; −1000≤w≤1000-1000\le w\le 1000; không có chu trình trọng số âm.
  • Subtask 3 (50 points): 1≤n≤2001\le n\le 200; 0≤m≤50000\le m\le 5000; 1≤u,v≤n1\le u,v\le n; −1000≤w≤1000-1000\le w\le 1000; không có chu trình trọng số âm.

Examples

Input

4 4
1 2 2
1 3 5
3 2 -10
2 4 3

Output

0 -5 5 -2

Explanation

Vertex 33 is reached at distance 55. Although the direct edge 1→21\to2 has length 22, the route 1→3→21\to3\to2 has length 5−10=−55-10=-5. Following 2→42\to4 gives distance −5+3=−2-5+3=-2 to vertex 44. The distance to vertex 11 itself is 00.