PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000013. Đường đi ngắn nhất 3 trạng thái (Shortest Path 3)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Đườ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 nn vertices numbered 00 through n−1n-1 and a source vertex ss. 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 vv, distinguish three possible outcomes. If vv cannot be reached from ss, print Impossible. If a reachable negative cycle can be traversed and then vv 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 n,m,q,sn,m,q,s, followed by mm directed edges u,v,wu,v,w and qq vertex queries.

Output

For each query print Impossible, -Infinity, or a finite integer distance.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 0≤m≤500\le m\le50; 1≤q≤10001\le q\le1000; 0≤s,u,v,x<n0\le s,u,v,x<n; −1000≤w≤1000-1000\le w\le1000.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 0≤m≤5000\le m\le500; 1≤q≤10001\le q\le1000; 0≤s,u,v,x<n0\le s,u,v,x<n; −1000≤w≤1000-1000\le w\le1000.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le1000; 0≤m≤100000\le m\le10000; 1≤q≤10001\le q\le1000; 0≤s,u,v,x<n0\le s,u,v,x<n; −1000≤w≤1000-1000\le w\le1000.

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.