## CCC '21 S4 - Daily Commute

View as PDF

Points: 12 (partial)
Time limit: 3.0s
Memory limit: 1G

Author:
Problem type
##### Canadian Computing Competition: 2021 Stage 1, Senior #4

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 of which allows you to walk from station to a different station in 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 at 6am in the morning and taking 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 day, the station and 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 day, starting at 6am in the morning (after the 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 marks are distributed.

marks
marks
marks
marks

#### Output Specification

The output is lines, with one integer per line. The line is the minimum number of minutes required to reach station on the day .

#### Sample Input

4 3 3
1 2
3 4
4 1
1 4 3 2
3 4
4 2
3 2

#### Output for Sample Input

1
2
3

#### Explanation 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 minute.

On the second day, the route will become , and you should take the subway to station (taking minute) and then walk to station (taking more minute).

On the third day, the route will become . One choice of optimal trip involves walking to station (taking minute), then boarding the train there and taking it through station and finally to station (taking another minutes).