#BF0000003. Cỗ máy thời gian (Time Machine)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 cities and directed bus routes. A route takes minutes; travel time may be negative because of time travel. You start in city at time zero.
Find the earliest arrival times for cities . If a negative-total-time cycle is reachable from city , the time can be reduced indefinitely, and you must print only a single -1. A cycle unreachable from city does not trigger that special output.
Input
First line: . Next lines: , vertices 1 through .
Output
Print a single -1 for a reachable negative cycle, else distance lines for vertices 2..n (use -1 if unreachable).
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
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.