#BF0000001. Ford–Bellman (Ford-Bellman)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 vertices numbered through and 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 . 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: . Next lines: , vertices 1 through .
Output
Print space-separated distances, or 30000 if unreachable.
Subtasks
- Subtask 1 (20 points): ; ; ; no negative-weight cycles; parallel edges and self-loops allowed.
- Subtask 2 (30 points): ; ; ; no negative-weight cycles; parallel edges and self-loops allowed.
- Subtask 3 (50 points): ; ; ; 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.