#BF0000015. Đường đi ngắn nhất toàn đồ thị (The Shortest Path)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi ngắn nhất toàn đồ thị (The Shortest Path)
The Shortest Path
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given a directed weighted graph, consider all ordered pairs of distinct vertices for which a walk from to exists. Find the minimum, over these pairs, of the shortest-walk cost. Walks may repeat vertices. If a negative cycle makes the minimum unbounded below, report negative infinity. Empty walks from a vertex to itself must not be counted.
Input
The first line contains . Then follow lines for edges with cost . Self-loops are forbidden, but parallel edges are allowed.
Output
Print -inf when the answer is unbounded below; otherwise print the minimum cost between distinct vertices.
Subtasks
-
Subtask 1 (20 points): ; ; ; on each edge; parallel edges allowed.
-
Subtask 2 (30 points): ; ; ; on each edge; parallel edges allowed.
-
Subtask 3 (50 points): ; ; ; on each edge; parallel edges allowed.
Examples
Input
4 4
1 2 5
2 3 -3
3 4 -3
1 4 2
Output
-6
Explanation
The walk costs , the minimum over all ordered pairs of distinct vertices.