Choosing a meeting place (Large)
InterviewTime limit5sMemory limit512 MB
Each friend moves at their own speed, so run Dijkstra once per friend and pick the city with the smallest worst arrival time.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Friends who live in different cities want to gather in one place. The roads are tangled and the friends live far apart, so it is hard to tell how much time to set aside. Find the smallest time it takes for all of them to meet in one city.
The map holds cities and roads. A road here is not a single link between two cities. It is a chain of consecutive links that runs through several cities in order.
Each test case gives the following.
- : the number of cities
- : the number of friends
- : the number of roads
Cities are numbered through .
For each friend , numbered through , the input gives the following.
- : the number of the city the friend starts from
- : the time the friend takes to move a distance of
For each road , the input gives the following.
- : the distance between neighbouring cities on that road. On one road every pair of neighbouring cities is the same distance apart.
- : the number of cities the road runs through
- : the numbers of those cities, in order
All friends leave at the same moment. Find the smallest time needed for every friend to gather in one city, and print -1 instead when no city can hold all of them.
A meeting happens only in a city, and a friend who arrives early can wait for the others.
Across all roads, a direct link between the same two cities appears at most once. On reaching a city, a friend can change between the roads that run through that city with no extra time.
Input
The first line holds the number of test cases . Each test case has the following form.
N P M
X_1 V_1
X_2 V_2
...
X_P V_P
D_1 L_1 C_{1,1} C_{1,2} ... C_{1,L_1}
D_2 L_2 C_{2,1} C_{2,2} ... C_{2,L_2}
...
D_M L_M C_{M,1} C_{M,2} ... C_{M,L_M}
Constraints
- The answer for each test case is at most .
Output
For each test case print one line in the form Case #x: y, where is the test case number starting from and is the answer for that case. When the friends cannot all meet in one city, print -1 in place of .
Hint

The picture shows the second test case of the sample input. A square is a city, a smiling face is a friend, the solid line is road 1 with distance , and the dashed line is road 2 with distance .