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

#BF0000003. Cỗ máy thời gian (Time Machine)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cỗ máy thời gian (Time Machine)

Time Machine

Source: BOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Find shortest travel times from city 1 to cities 2 through n. Print a single -1 for a reachable negative cycle.

There are nn cities and mm directed bus routes. A route u→vu\to v takes ww minutes; travel time may be negative because of time travel. You start in city 11 at time zero.

Find the earliest arrival times for cities 2,3,…,n2,3,\ldots,n. If a negative-total-time cycle is reachable from city 11, the time can be reduced indefinitely, and you must print only a single -1. A cycle unreachable from city 11 does not trigger that special output.

Input

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

Output

Print a single -1 for a reachable negative cycle, else n−1n-1 distance lines for vertices 2..n (use -1 if unreachable).

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤m≤501\le m\le50; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 1≤m≤5001\le m\le500; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 3 (50 points): 1≤n≤5001\le n\le500; 1≤m≤60001\le m\le6000; −10000≤w≤10000-10000\le w\le10000.

Examples

Input

3 3
1 2 4
2 3 -5
1 3 8

Output

4
-1

Explanation

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