#BF0000002. Đường đi ngắn nhất với cạnh âm (Shortest Path with Negative Edges)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi ngắn nhất với cạnh âm (Shortest Path with Negative Edges)
Shortest Path with Negative Edges
Source: AOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Find shortest paths from source r, or print NEGATIVE CYCLE if a negative cycle is reachable from r.
You are given a directed weighted graph with vertices , and a specified source vertex . Edge weights may be negative, and some vertices may be unreachable from .
If a negative-weight cycle can be reached from , path costs can be decreased without bound along its reachable region. In this case, print NEGATIVE CYCLE instead of distances. Otherwise, print the shortest path distance from to each vertex, using INF where no path exists. A negative cycle disconnected from the source does not trigger the special message.
Input
First line: . Next lines: , vertices from 0 through .
Output
Print NEGATIVE CYCLE if reachable, else lines containing distances or INF.
Subtasks
- Subtask 1 (20 points): ; ; ; no self-loops or duplicate ordered edges; source 0..n−1.
- Subtask 2 (30 points): ; ; ; no self-loops or duplicate ordered edges; source 0..n−1.
- Subtask 3 (50 points): ; ; ; no self-loops or duplicate ordered edges; source 0..n−1.
Examples
Input
4 6 0
0 1 2
0 2 3
1 2 -5
1 3 1
2 3 2
3 1 0
Output
NEGATIVE CYCLE
Explanation
Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.