#P16487. [GKS 2014 #C] Taking Metro

    ID: 16472 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2014最短路Google Kick Start

[GKS 2014 #C] Taking Metro

题目描述

Tom is taking metros in the city to go from station to station.

The metro system in the city works like this:

  • There are NN metro lines in the city: line 11, line 22, ..., line NN.
  • For each metro i, there are SNiSN_i stations. Let's assume they are Si,1,Si,2,,Si,SNiS_{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 Si,1S_{i,1} \to Si,2S_{i,2} \to ... \to Si,SNiS_{i,SN_i}, and Si,SNiS_{i,SN_i} \to Si,SNi1S_{i,SN_i-1} \to ... \to Si,1S_{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 Timei,1Time_{i,1} minutes to travel from Si,1S_{i,1} to Si,2S_{i,2}, Timei,2Time_{i,2} minutes to travel from Si,2S_{i,2} to Si,3S_{i,3}, etc. It takes the same time in the other direction.
  • There are MM 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 WiW_i minutes for the next metro.

Now, you are going to travel from one station to another. Find out the shortest time you need.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case starts with an integer NN, the number of metro lines. NN metros descriptions follow. Each metro description starts with two integers SNiSN_i and WiW_i, the number of stations and the expected waiting time in minutes. The next line consists of SNi1SN_i-1 integers, Timei,1Time_{i,1}, Timei,2Time_{i,2}, ..., Timei,SNi1Time_{i,SN_i-1}, describing the travel time between stations.

After the metro descriptions, there is an integer MM, the number of tunnels. MM lines follow to describe the tunnels. Each tunnel description consists of 5 integers, m1im1_i, s1is1_i, m2im2_i, s2is2_i, tit_i which means the tunnel is connecting stations Sm1i,s1iS_{m1_i,s1_i} and station Sm2i,s2iS_{m2_i,s2_i}. The walking time of the tunnel is tit_i.

The next line contains an integer QQ, the number of queries. Each of the next QQ lines consists of 4 integers, x1x1, y1y1, x2x2, y2y2, which mean you are going to travel from station Sx1,y1S_{x1,y1} to station Sx2,y2S_{x2,y2}.

输出格式

For each test case, output one line containing "Case #x:", where xx is the test case number (starting from 11), then followed by QQ lines, each line containing an integer yy which is the shortest time you need for that query. If it's impossible, output 1-1 for that query instead.

2

2
5 3
3 5 7 3
4 2
1 1 1
1
1 2 2 2 1
1
1 1 2 4

2
5 3
3 5 7 3
4 2
1 1 1
2
1 2 2 2 1
2 4 1 4 1
1
1 1 1 5
Case #1:
 11
Case #2:
 18

提示

Limits

1T1001 \le T \le 100.

1Wi1001 \le W_i \le 100.

1Timei,j1001 \le Time_{i,j} \le 100.

1m1iN1 \le m1_i \le N.

1s1iSNm1i1 \le s1_i \le SN_{m1_i}.

1m2iN1 \le m2_i \le N.

1s2iSNm2i1 \le s2_i \le SN_{m2_i}.

m1im1_i and m2im2_i will be different.

1ti1001 \le t_i \le 100.

1Q101 \le Q \le 10.

1x1N1 \le x1 \le N.

1y1SNx11 \le y1 \le SN_{x1}.

1x2N1 \le x2 \le N.

1y2SNy21 \le y2 \le SN_{y2}.

Station Sx1,y1S_{x1,y1} and station Sx2,y2S_{x2,y2} will be different.

Small dataset (Test Set 1 - Visible)

1N101 \le N \le 10.

0M100 \le M \le 10.

2SNi1002 \le SN_i \le 100.

The total number of stations in each case is at most 100100.

Large dataset (Test Set 2 - Hidden)

1N1001 \le N \le 100.

0M1000 \le M \le 100.

2SNi10002 \le SN_i \le 1000.

The total number of stations in each case is at most 10001000.