Find the fastest route between two metro stations, combining per-line boarding waits, ride times, and tunnel walks.
Medium4Shortest pathGraphNo attempts yetTime limit5sMemory limit512 MBTom rides the metro to get from one station to another.
The metro system works like this.
You are going to travel from one station to another. Find the smallest possible time.
The first line contains the number of test cases, T. T test cases follow.
Each test case starts with a line containing the number of metro lines, N. Descriptions of N lines follow. Each line description starts with a row holding the number of stations SNi and the waiting time Wi. The next row holds SNi−1 integers Timei,1,Timei,2,…,Timei,SNi−1, the travel times between neighbouring stations.
After the line descriptions comes a row holding the number of tunnels, M. Each of the next M rows holds 5 integers m1i, s1i, m2i, s2i, ti: the tunnel connects station Sm1i,s1i and station Sm2i,s2i, and walking through it takes ti minutes.
The next row holds the number of queries, Q. Each of the next Q rows holds 4 integers x1, y1, x2, y2, meaning you travel from station Sx1,y1 to station Sx2,y2.
Limits
For each test case, first print Case #x:, where x is the test case number starting from 1. Then print Q lines, one per query in the order given. Each line holds the smallest time for that query as an integer, or -1 if the trip is impossible.
In the first test case of the first example you travel from station 1 of line 1 to station 4 of line 2. The fastest way is:
The time spent is 3+3+1+2+2=11 minutes.