Elevators
Time limit2sMemory limit256 MB
Find the shortest total ride distance from the start floor to the target floor by switching between elevators that each serve only certain floors.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
The new building of the computer engineering department has several elevators and no stairs. To make lecture rooms and offices easier to reach, the manufacturer set each elevator to stop only at certain predefined floors. Some elevators stop only at odd floors, some only at even floors. It goes further than that: the buttons inside and outside an elevator work only for the floors that elevator is assigned to stop at. Faculty members now reach their destination floors faster, but people who do not know the building, students in particular, get confused.
A person is on floor and wants to go to floor . To spend the least travel time, which elevator should take, and on which floors should transfer to which elevators? Write 's travel path as . The travel time to minimize is
One leg of the path is possible only when a single elevator stops at both and . Write a program that helps the people who use these elevators.
Input
The input holds several test cases. The first line of a test case gives the number of elevators (), then the source floor and the destination floor. Line of the next lines starts with (), the number of floors at which elevator may stop, followed by floor numbers. Every floor number is a non-negative integer smaller than 150. The floor numbers are not guaranteed to be sorted.
The input ends with a line containing 0 0 0, which you do not process.
Output
For each test case, print on one line the minimum travel time needed to reach the destination floor from the source floor. A way from the source floor to the destination floor always exists.