P16487 [GKS 2014 #C] Taking Metro
Description
Tom is taking metros in the city to go from station to station.
The metro system in the city works like this:
* There are $N$ metro lines in the city: line $1$, line $2$, ..., line $N$.
* For each metro i, there are $SN_i$ stations. Let's assume they are $S_{i,1}, S_{i,2}, \dots, S_{i,SN_i}$. These stations are ordered from one end point to the other end point. The metro is running in both directions. In other words, the metro is going from $S_{i,1}$ $\to$ $S_{i,2}$ $\to$ ... $\to$ $S_{i,SN_i}$, and $S_{i,SN_i}$ $\to$ $S_{i,SN_i-1}$ $\to$ ... $\to$ $S_{i,1}$. You can take the metro from any station and get off at any station. It takes a certain time to travel from one station to the next station. It takes $Time_{i,1}$ minutes to travel from $S_{i,1}$ to $S_{i,2}$, $Time_{i,2}$ minutes to travel from $S_{i,2}$ to $S_{i,3}$, etc. It takes the same time in the other direction.
* There are $M$ transfer tunnels. Each transfer tunnel connects two stations of different metro lines. It takes a certain amount of time to travel through a tunnel in either direction. You can get off the metro at one end of the tunnel and walk through the tunnel to the station at the another end.
* When you arrive at a metro station of line i, you need to wait $W_i$ minutes for the next metro.
Now, you are going to travel from one station to another. Find out the shortest time you need.
Input Format
The first line of the input gives the number of test cases, $T$. $T$ test cases follow.
Each test case starts with an integer $N$, the number of metro lines. $N$ metros descriptions follow. Each metro description starts with two integers $SN_i$ and $W_i$, the number of stations and the expected waiting time in minutes. The next line consists of $SN_i-1$ integers, $Time_{i,1}$, $Time_{i,2}$, ..., $Time_{i,SN_i-1}$, describing the travel time between stations.
After the metro descriptions, there is an integer $M$, the number of tunnels. $M$ lines follow to describe the tunnels. Each tunnel description consists of 5 integers, $m1_i$, $s1_i$, $m2_i$, $s2_i$, $t_i$ which means the tunnel is connecting stations $S_{m1_i,s1_i}$ and station $S_{m2_i,s2_i}$. The walking time of the tunnel is $t_i$.
The next line contains an integer $Q$, the number of queries. Each of the next $Q$ lines consists of 4 integers, $x1$, $y1$, $x2$, $y2$, which mean you are going to travel from station $S_{x1,y1}$ to station $S_{x2,y2}$.
Output Format
For each test case, output one line containing "Case #x:", where $x$ is the test case number (starting from $1$), then followed by $Q$ lines, each line containing an integer $y$ which is the shortest time you need for that query. If it's impossible, output $-1$ for that query instead.
Explanation/Hint
**Limits**
$1 \le T \le 100$.
$1 \le W_i \le 100$.
$1 \le Time_{i,j} \le 100$.
$1 \le m1_i \le N$.
$1 \le s1_i \le SN_{m1_i}$.
$1 \le m2_i \le N$.
$1 \le s2_i \le SN_{m2_i}$.
$m1_i$ and $m2_i$ will be different.
$1 \le t_i \le 100$.
$1 \le Q \le 10$.
$1 \le x1 \le N$.
$1 \le y1 \le SN_{x1}$.
$1 \le x2 \le N$.
$1 \le y2 \le SN_{y2}$.
Station $S_{x1,y1}$ and station $S_{x2,y2}$ will be different.
**Small dataset (Test Set 1 - Visible)**
$1 \le N \le 10$.
$0 \le M \le 10$.
$2 \le SN_i \le 100$.
The total number of stations in each case is at most $100$.
**Large dataset (Test Set 2 - Hidden)**
$1 \le N \le 100$.
$0 \le M \le 100$.
$2 \le SN_i \le 1000$.
The total number of stations in each case is at most $1000$.