Meeting Point (Small)
InterviewTime limit5sMemory limit512 MB
Friends with different speeds start from given cities and must meet in one city, so minimize the worst arrival time over all cities.
- Level
Medium4 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 judge how much time to set aside. Find the smallest amount of time the friends need to meet in a single city.
The map holds cities and roads. A road here is not a single link between two cities. It is a continuous route that passes through several cities in order.
Each test case gives the number of cities , the number of friends , and the number of roads . Cities are numbered through .
For each friend () you get:
- : the city the friend starts from.
- : the time the friend takes to move a distance of .
For each road () you get:
- : the distance between neighbouring cities on that road. On one road every neighbouring pair sits the same distance apart.
- : the number of cities the road passes through.
- : the cities the road passes through, in order. and are joined directly by a path of length .
Every friend leaves at the same moment. Find the smallest time in which all of them can be in one city. If no city can hold all of them, print -1 instead of a time.
A meeting happens only in a city, and a friend who arrives early can wait for the others. No two cities are joined directly by more than one path. Once a friend reaches a city, that friend can switch between the roads passing through it at no extra cost.
Input
The first line has the number of test cases . Each test case follows in this format.
The first line holds , , and , separated by spaces. The next lines hold and . The next lines hold , , then through .
Constraints
- and
- 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 case number starting at and is the answer for that case. If the friends cannot all meet in one city, print -1 in place of .