PivotOJ

Buggy Blinkers

시간 제한: 4000ms메모리 제한: 1024MB출처: BAPC 2024BOJ 32596

문제

Recently, your car underwent a software update. Now, if you use the blinkers too much, the car shuts down, reporting a "buffer overflow", whatever that means! On the bright side, you are now welcome at the Broken-down Automobile Preservation Convention (BAPC).

You found out late, so you have to drive there as quickly as possible! Still, of course, you have to obey all traffic rules. At each intersection, you should follow these rules, regardless of whether an intersection has roads in all directions or not:

  • When turning left (or right) at an intersection, the left (or right) blinker must be on.
  • When driving straight ahead, the blinkers must be off.
  • U-turns are not allowed, i.e. you are not allowed to turn back the way you came.

To play it safe with your blinkers, you decide you are going to activate them at most kk times. Luckily, you can still deactivate them at any time. This seems rather limiting, but you make one shrewd observation: as long as you keep your blinkers on (they do not turn off automatically), you can keep turning in the same direction.

The road network consists of intersections with roads between them. Roads always start and end in one of the four cardinal directions: north, east, south, or west. Furthermore, they never start and end at the same intersection. As an example, consider sample cases 1 through 3, visualized in Figure B.1. These samples only differ in their value of kk.

Figure B.1: Visualization of the first, second, and third sample input.

To simplify navigation, you assume that each road can be traversed in the same amount of time, i.e. each road is considered to be of length 11. Find the shortest route from your current location to the BAPC, ensuring that you do not activate the blinkers more than kk times. From your current location, you can drive in any direction without using your blinkers.

입력

The input consists of:

  • One line with two integers nn and kk (1n50001 \le n \le 5000, 0k200 \le k \le 20), the number of intersections and the number of times the blinkers can be activated.
  • nn lines, the iith of which contains four integers viNv_i^{\text{N}}, viEv_i^{\text{E}}, viSv_i^{\text{S}}, and viWv_i^{\text{W}} (0viN,viE,viS,viWn0 \le v_i^{\text{N}}, v_i^{\text{E}}, v_i^{\text{S}}, v_i^{\text{W}} \le n), the intersections that can be reached by taking the north, east, south, and west road from intersection ii, or 00 to indicate that the road does not exist.

You start at intersection 11, and the BAPC is located at intersection nn. Each intersection ii has at most one road to each other intersection jj. If this road exists, then intersection jj has exactly one road to intersection ii as well.

출력

If it is possible to drive from intersection 11 to nn using the blinkers at most kk times, output the length of the shortest such route. Otherwise, output "impossible".

예제

예제 1

입력
5 2
0 2 0 0
4 0 3 1
0 4 2 0
0 5 2 3
0 0 0 4
출력
3

예제 2

입력
5 1
0 2 0 0
4 0 3 1
0 4 2 0
0 5 2 3
0 0 0 4
출력
4

예제 3

입력
5 0
0 2 0 0
4 0 3 1
0 4 2 0
0 5 2 3
0 0 0 4
출력
impossible

예제 4

입력
5 2
0 2 0 0
4 0 3 1
0 4 2 0
0 0 2 3
0 0 0 0
출력
impossible
코드를 제출하려면 로그인하세요.