You are on page 1of 3

Olimpiadi Italiane di Informatica 2022

Biella, 23 settembre 2022 bus • EN

Episodio II: un lungo viaggio (bus)


⎐⌇⟟⋔🝥⟟⏃⎅⟟ finally landed in the Baikonur Cos-
modrome1 with his delegation. Many OIIs took
place during the space travel, and it’s now time
to get to Biella for the 2022 edition!
Unfortunately, travelling on Earth is not easy
for an alien. The spaceship is not suitable for
such short travels, walking is tiring and taking
public transport can raise many questions, lead-
ing to bureaucratic inspections, interrogations,
Figure 1: Bus line 26, headed to Biella central.
scientific analysis and vivisections.
For this reason, ⎐⌇⟟⋔🝥⟟⏃⎅⟟ chose to only employ buses, taking as much time as needed (he is months
ahead of schedule!), but making as few changes as possible to reduce the amount of inspections.
There are N bus stops on Earth, indexed from 0 to N − 1. The Baikonur Cosmodrome is in front of stop
0, while ITIS Quintino Sella, Biella, is next to stop N − 1. There are L different bus lines. The i-th line
(0 ≤ i < L) makes Ki stops, namely Fi,0 , Fi,1 , . . . , Fi,Ki −1 , in this order. In other words, bus i leaves
from stop Fi,0 , makes Ki − 2 intermediate stops, and ends in stop Fi, Ki −1 . Note that lines are one-way
and, for each line, there may not be a line making the same stops in reverse order.
⎐⌇⟟⋔🝥⟟⏃⎅⟟ wants to take a bus at stop 0, make as few changes as possible (possibly 0), and finally get
off at stop N − 1. A change consists in getting off the current bus at a certain stop j and getting on
another bus (possibly of the same line) at the same stop. Help ⎐⌇⟟⋔🝥⟟⏃⎅⟟ plan his travel in the best
way possible, or tell that stop N − 1 is unreachable by the available bus lines.

Implementation
You should submit a single file, with a .cpp extension.

☞ Among the attachments in this task you will find a template bus.cpp with a sample imple-
mentation.

You will have to implement the following function:

C++ int pianifica(int N, int L, vector<vector<int>> F);

• The integer N is the number of stops.


• The integer L is the number of bus lines.
• The vector of vectors F , indexed from 0 to L − 1, contains the details of the bus stops. Specifically,
for 0 ≤ i ≤ L − 1, F [i] is a vector of size Ki containing the integers Fi,0 , Fi,1 , . . . , Fi,Ki −1 in order.
• The function must return the minimum number of changes necessary for ⎐⌇⟟⋔🝥⟟⏃⎅⟟ to reach stop
N − 1 from stop 0, or −1 if this is impossible.

Sample Grader
Among this task’s attachments you will find a simplified version of the grader used during evaluation,
which you can use to test your solutions locally. The sample grader reads data from stdin, calls the
function that you should implement and writes back on stdout using the following format.
1
The biggest spaceport currently active on Earth, located in Kazakhstan.

bus Page 1 of 3
The input file is made up of L + 1 lines, containing:
• Line 1: the two space-separated integers N and L.
• Line 1 + i (0 ≤ i ≤ L − 1): the integer Ki , followed by Ki integers Fi,0 , . . . , Fi,Ki −1 .
The output file contains a single line, containing the return value S of function pianifica.

Constraints
• 2 ≤ N ≤ 100 000.
• 1 ≤ L ≤ 100 000.
• Ki ≥ 2 for each 0 ≤ i < L.
• K0 + K1 + · · · + KL−1 ≤ 300 000.
• 0 ≤ Fi,j < N for each 0 ≤ i < L and 0 ≤ j < Ki .
• Fi,j ̸= Fi,j+1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1.

Scoring
Your program will be tested on a number of testcases grouped in subtasks. In order to obtain the score
associated to a subtask, you need to correctly solve all the testcases it contains.
• Subtask 1 [ 0 points]: Sample cases.
• Subtask 2 [17 points]: Ki = 2 for each 0 ≤ i < L.
• Subtask 3 [20 points]: N ≤ 2000, and K0 + K1 + · · · + KL−1 ≤ 4000.
• Subtask 4 [19 points]: Fi,j < Fi,j+1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1.
• Subtask 5 [12 points]: Ki is odd and Fi,j = Fi,Ki −j−1 for each 0 ≤ i < L and 0 ≤ j < Ki − 1
(each bus serves its first-half stops again in the opposite direction).
• Subtask 6 [14 points]: No station appears more than 50 times in the input.
• Subtask 7 [18 points]: No additional constraints.

Examples
stdin stdout

8 3 3
7 2 3 6 1 4 3 5
2 6 7
2 0 1

4 2 -1
4 2 3 1 0
2 0 1
stdin stdout

5 3 0
5 0 3 1 2 4
2 1 4
2 0 1

bus Page 2 of 3
Explanation
The optimal solution for the first sample case consists in:
• Taking line 2 at bus stop 0;
• Changing to line 0 at stop 1;
• Getting off the bus and back on line 0 at stop 3;
• Changing to line 1 at stop 6;
• Finally getting off the bus at stop 7.
This trip makes a total of 3 changes.

4 3 5

0 1 6 7

Figure 2: First sample case

In the second sample case, it is impossible to reach stop 3 starting from stop 0.
In the third sample case, the optimal solution consists in taking line 0 from stop 0 to stop 4, without
any changes on the way. Note that it is also possible to reach stop 4 using the line 2 and changing at
stop 1 to line 1. This solution however requires more changes and is not optimal.
2 2

0 1
0 3
4

1 3
Figure 3: Second sample case Figure 4: Third sample case

bus Page 3 of 3

You might also like