#P4011. 孤岛营救问题

    ID: 2944 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>O2优化广度优先搜索,BFS状态压缩,状压图的建立,建图进制网络流 24 题

孤岛营救问题

Description

In the year 19441944, special forces soldier Mike received an order from the Ministry of Defense to rush to a deserted island in the Pacific to rescue Private Ryan, who had been captured by the enemy. Ryan is imprisoned in a maze. The maze is complex, but fortunately Mike has obtained a map of it. The maze is rectangular, divided into NN rows from north to south and MM columns from west to east, so the entire maze consists of N×MN \times M cells. The position of each cell is represented by an ordered pair (row, column). Two adjacent cells in the north-south or east-west direction may be connected, or there may be a locked door between them, or an impassable wall. Some cells contain keys. All doors are divided into PP classes; keys that open doors of the same class are identical, and keys for different classes are different.

Private Ryan is held in the southeast corner of the maze, i.e., in cell (N,M)(N, M), and is unconscious. The maze has only one entrance at the northwest corner. That is, Mike can directly enter cell (1,1)(1, 1). The time for Mike to move from one cell to an adjacent cell is 11. The time to pick up a key in the current cell and to unlock a door is negligible.

Design an algorithm to help Mike reach the cell where Ryan is as quickly as possible and rescue him.

Input Format

  • The first line contains 33 integers, representing the values of NN, MM, and PP.
  • The second line contains 11 integer KK, the total number of doors and walls in the maze.
  • The I+2I+2-th line (1IK1 \leq I \leq K) contains 55 integers, in order Xi1,Yi1,Xi2,Yi2,GiX_{i1}, Y_{i1}, X_{i2}, Y_{i2}, G_i:
    • When Gi1G_i \geq 1, there is a class-GiG_i door between cells (Xi1,Yi1)(X_{i1}, Y_{i1}) and (Xi2,Yi2)(X_{i2}, Y_{i2}).
    • When Gi=0G_i = 0, there is an impassable wall between cells (Xi1,Yi1)(X_{i1}, Y_{i1}) and (Xi2,Yi2)(X_{i2}, Y_{i2}) (where Xi1Xi2+Yi1Yi2=1|X_{i1} - X_{i2}| + |Y_{i1} - Y_{i2}| = 1, 0GiP0 \leq G_i \leq P).
  • The K+3K+3-th line contains an integer SS, the total number of keys in the maze.
  • The K+3+JK+3+J-th line (1JS1 \leq J \leq S) contains 33 integers, in order Xj,Yj,QjX_{j}, Y_{j}, Q_j: the JJ-th key is placed in cell (Xj,Yj)(X_{j}, Y_{j}), and it opens doors of class QjQ_j (where 1QjP1 \leq Q_j \leq P).

Adjacent integers on the same line in the input are separated by a single space.

Output Format

Output the minimum time for Mike to rescue Private Ryan. If the problem has no solution, output 1-1.

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1
14

Hint

Xi1Xi2+Yi1Yi2=1|X_{i1} - X_{i2}| + |Y_{i1} - Y_{i2}| = 1, 0GiP0 \leq G_i \leq P.

1QjP1 \leq Q_j \leq P.

Constraints: N,M,P10N, M, P \leq 10, K<150K < 150, S14S \leq 14.

Translated by ChatGPT 5