PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000054. Đường đi ngắn nhất mọi cặp (All Pairs Shortest Path)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đường đi ngắn nhất mọi cặp (All Pairs Shortest Path)
All Pairs Shortest Path
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Answer shortest-path queries on a directed weighted graph allowing negative edges. If no route exists, report Impossible; if a reachable negative cycle can be visited en route, report -Infinity; otherwise report the finite shortest distance.
Input
First n m q; then m directed edges u v w (0-based), then q lines s t.
Output
For each query print an integer, Impossible, or -Infinity, with this exact capitalization.
Subtasks
- Subtask 1 (40 points): $1\le n\le 8,\ 0\le m\le 25,\ 1\le q\le 15,\ |w|\le 20$. All indices u,v,s,t are 0-based. The graph is directed; self-loops and parallel edges are allowed.
- Subtask 2 (60 points): $1\le n\le 100,\ 0\le m\le 600,\ 1\le q\le 100,\ |w|\le 10^5$. All indices u,v,s,t are 0-based. The graph is directed; self-loops and parallel edges are allowed.
Examples
Input
3 3 4
0 1 4
1 2 -6
2 1 2
0 2
2 0
0 0
1 1
Output
-Infinity
Impossible
0
-Infinity
Explanation
Cycle 1→2→1 has weight -4. From 0 to 2 this cycle can be repeated arbitrarily, giving -Infinity. Vertex 2 cannot reach 0. The empty 0→0 path costs 0, while 1→1 can traverse the negative cycle.