Find the fastest route between two metro stations where each boarding adds a line waiting time and transfers use walking tunnels.
Medium5Shortest pathGraphNo attempts yetTime limit5sMemory limit512 MBTom rides the city metro to get from one station to another.
The metro system works like this.
You start at one station and travel to another one. Find the shortest total time.
The first line holds the number of test cases T. The test cases follow.
Each test case starts with a line holding N, the number of metro lines. Descriptions of the N lines follow. The description of line i starts with a line holding two integers SNi and Wi, the number of stations and the waiting time in minutes. The next line holds SNi−1 integers Timei,1,Timei,2,…,Timei,SNi−1, the riding times between neighboring stations.
After the line descriptions comes a line holding M, the number of transfer tunnels, then M lines. Each of them holds five integers m1i, s1i, m2i, s2i, ti, meaning that a tunnel connects station Sm1i,s1i and station Sm2i,s2i, and that walking through that tunnel takes ti minutes.
The next line holds Q, the number of queries, followed by Q lines. Each of them holds four integers x1, y1, x2, y2, meaning that you travel from station Sx1,y1 to station Sx2,y2.
Limits:
For each test case, print one line holding Case #x:, where x is the test case number starting from 1. Then print Q lines. The k-th of them holds the shortest time for the k-th query, or -1 when the destination cannot be reached.
In the first query of the first test case of the sample input you go from station 1 of line 1 to station 4 of line 2. One shortest route is:
The total time is 3 + 3 + 1 + 2 + 2 = 11.