#BF0000013. Đường đi ngắn nhất 3 trạng thái (Shortest Path 3)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi ngắn nhất 3 trạng thái (Shortest Path 3)
Shortest Path 3
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
For each vertex query, output Impossible, a finite distance, or -Infinity if a negative cycle affects the vertex.
You are given a directed graph with vertices numbered through and a source vertex . Edge weights are integers and may be negative, so a shortest-path distance might be undefined because of a reachable negative cycle.
For each queried vertex , distinguish three possible outcomes. If cannot be reached from , print Impossible. If a reachable negative cycle can be traversed and then reached, the cost has no lower bound; print -Infinity. Otherwise, print the finite shortest-path distance. A negative cycle somewhere in the graph need not affect all destinations.
Input
First line , followed by directed edges and vertex queries.
Output
For each query print Impossible, -Infinity, or a finite integer distance.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; .
Examples
Input
6 5 5 0
0 1 1
1 2 -3
2 1 1
2 3 2
0 4 7
0
1
3
4
5
Output
0
-Infinity
-Infinity
7
Impossible
Explanation
Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.