Problem S4: Daily Commute
2021 · S4 · Original problem · DMOJProblem Description
Toronto has subway stations, numbered from to . You start at station , and every day, you need to reach station to get to school.
There are one-way walkways running amongst the stations, the th of which allows you to walk from station to a different station in 1 minute. There may be multiple walkways connecting any given pair of stations.
The subway line follows a certain route through the stations, starting at station and visiting each station once. Initially, this route consists of stations , in that order. , and is a permutation of the integers . Only one subway train runs along this route per day, departing from station 1 at 6am in the morning and taking 1 minute to reach each subsequent station. This means that, minutes after 6am, the train will be at station (or at station if ).
Over a period of days, however, the subway line’s route will keep changing. At the start of the th day, the th station and th station in the route will be swapped. Note that, after each such change, the route will still begin at station and will visit all stations once each. Changes will carry over to subsequent days – the route will not automatically reset itself back to .
On each of these days, you’d like to determine how quickly you can get to school so you can begin learning things. On the th day, starting at 6am in the morning (after the th update to the subway line’s route), you’ll begin your daily trip to station . Each minute, you may either ride the subway to its next stop (if you’re currently at the same station as the train and it hasn’t already completed its route), take a walkway from your current station to another one, or wait at your current station. Note that your trip begins at the same time as the train’s route, meaning that you may choose to immediately ride it if you’d like to, and that you may choose to leave and then get back on the train during your trip.
Input Specification
The first line contains three space-separated integers, , , and .
The next lines each contain two space-separated integers, and .
The next line contains the space-separated integers, , which form the initial permutation of stations.
The next lines each contain two space-separated integers, and .
The following table shows how the available 15 marks are distributed.
| 2 marks | |||
| 2 marks | |||
| 3 marks | |||
| 8 marks |
Output Specification
The output is lines, with one integer per line. The th line is the minimum number of minutes required to reach station on the th day ().
Sample Input
4 3 3
1 2
3 4
4 1
1 4 3 2
3 4
4 2
3 2Output for Sample Input
1
2
3Explanation of Output for Sample Input
At the start of the first day, the subway line’s route will be updated to visit stations , in that order. On that day, you should simply take the subway to station , taking 1 minute.
On the second day, the route will become , and you should take the subway to station (taking 1 minute) and then walk to station (taking 1 more minute).
On the third day, the route will become . One choice of optimal trip involves walking to station 2 (taking 1 minute), then boarding the train there and taking it through station 3 and finally to station 4 (taking another 2 minutes).