#P4011. 孤岛营救问题
孤岛营救问题
Description
In the year , 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 rows from north to south and columns from west to east, so the entire maze consists of 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 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 , and is unconscious. The maze has only one entrance at the northwest corner. That is, Mike can directly enter cell . The time for Mike to move from one cell to an adjacent cell is . 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 integers, representing the values of , , and .
- The second line contains integer , the total number of doors and walls in the maze.
- The -th line () contains integers, in order :
- When , there is a class- door between cells and .
- When , there is an impassable wall between cells and (where , ).
- The -th line contains an integer , the total number of keys in the maze.
- The -th line () contains integers, in order : the -th key is placed in cell , and it opens doors of class (where ).
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 .
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
, .
.
Constraints: , , .
Translated by ChatGPT 5
京公网安备 11011102002149号