Twilight
Time limit4sMemory limit1024 MB
Find the shortest route from city 1 to every city that never walks the consecutive roads of any of the K forbidden paths.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Ostania is a country with cities and roads connecting them. The cities are numbered from to . The -th road is a one-way road from city to city , and traveling along it takes time. Between any two distinct cities, there is at most one road in each direction. For convenience, let be the time needed to travel along the road from city to city .
Westalis is a country in a Cold War rivalry with Ostania. Through several agents, Westalis obtained pieces of information about Ostania. Each piece is represented by a simple path. Precisely, the -th piece is a sequence of distinct cities , and for every there is a road from city to city . No city belongs to two different pieces of information at the same time.
Twilight, the agent codenamed , is currently in city of Ostania. The Westalis government orders Twilight to travel to city to collect information. To maximize the efficiency of the collection, Twilight decides to take a route that contains none of the simple paths. Specifically, let be the cities Twilight visits in order. Then all of the following must hold.
- There is a road from city to city .
- There are no integers and such that .
- Among all routes that satisfy conditions 1 and 2, the total time to reach city , , is minimal.
For every integer with , determine whether Twilight can reach city , and if so, compute the time it takes to reach city .
Input
The first line contains three integers , , and separated by spaces.
Each of the next lines contains , , and separated by spaces. For each , the road from city to city is unique. is an integer
Line contains followed by the integers of the simple path, separated by spaces. For the simple paths, each city appears at most once, and it is guaranteed that the path can be traversed along the roads in the order given in the input.
Output
Print integers separated by spaces on the first line. The -th integer is the time it takes Twilight to reach city while satisfying all the conditions. If city cannot be reached, print -1.