Crossings of Crossings
InterviewTime limit2sMemory limit1024 MB
Find the fewest crossings needed to walk from house 0 on the left side to house f on the right side, given the crossings on each side and across the avenue.
- Level
Medium4 of 10
- Topics
- BFS, Shortest path, Graph
- Solved
- No attempts yet
Problem
Zhele must reach the city office of the Transylvania hotel. The route runs along a long avenue with many intersections, including T-shaped ones (where one side street is missing).
To help Zhele find the way, the zombies gave him a schematic map. All houses are numbered in order. Houses on opposite sides of the avenue share the same number. The map also marks every crossing on the avenue. There are three types of crossings:
- Along the left side of the avenue. A crossing lies between two houses and is given by the smaller house number . Crossing connects houses and .
- Along the right side of the avenue. A crossing lies between two houses and is given the same way.
- Across the avenue. It lets you go from one side to the other, and it lies between two houses on opposite sides. It is given by the number of those houses.
Zhele starts at house 0 on the left side. He can move toward higher or lower house numbers. The office is on the right side at house .
Zhele hates crossings, so he wants to minimize the number of crossings he uses on the way.
Input
The first line contains three natural numbers , , (): the number of crossings of the first, second, and third type.
The second line contains integers (): the crossings on the left side of the avenue.
The third line contains integers (): the crossings on the right side of the avenue.
The fourth line contains integers (): the crossings across the avenue.
The fifth line contains one integer (): the house number Zhele is heading to.
Output
Print one line with the minimum number of crossings Zhele must use to reach the office.