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

#BF0000001. Ford–Bellman (Ford-Bellman)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ford–Bellman (Ford-Bellman)

Ford-Bellman

Source: E-olymp

Version: Phuoc Hung OJ Extended

Problem Statement

Find shortest paths from vertex 1 in a directed graph without negative cycles. Print 30000 for unreachable vertices.

You are given a directed graph with nn vertices numbered 11 through nn and mm weighted edges. Edge weights are integers that may be positive, zero or negative. Parallel directed edges and self-loops are allowed.

The length of a path equals the sum of its edge weights. For each vertex, determine the smallest possible path length starting from vertex 11. The graph is guaranteed to have no negative-weight cycle, so every reachable vertex has a well-defined shortest distance. Print the sentinel 30000 for an unreachable vertex.

Input

First line: n,mn,m. Next mm lines: u,v,wu,v,w, vertices 1 through nn.

Output

Print nn space-separated distances, or 30000 if unreachable.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 0≤m≤500\le m\le50; −100≤w≤100-100\le w\le100; no negative-weight cycles; parallel edges and self-loops allowed.
  • Subtask 2 (30 points): 1≤n≤1001\le n\le100; 0≤m≤5000\le m\le500; −100≤w≤100-100\le w\le100; no negative-weight cycles; parallel edges and self-loops allowed.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le100; 0≤m≤100000\le m\le10000; −100≤w≤100-100\le w\le100; no negative-weight cycles; parallel edges and self-loops allowed.

Examples

Input

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

Output

0 10 11 30000

Explanation

Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.