#P13563. 「WWOI R1」WSM 游戏

    ID: 14406 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>O2优化广度优先搜索 BFS最短路状压 DP

「WWOI R1」WSM 游戏

Background

WSM\texttt{WSM} is an adventure game, and WSM is the main character of this game.

Problem Description

There is a map made of a grid with nn rows and mm columns. WSM starts from the top-left cell with coordinates (1,1)(1,1) and needs to reach the cell with coordinates (a,b)(a,b).

There are kk password locks and tt password keys on the map.
When WSM arrives at the cell containing a key with password rr, the lock with password rr disappears immediately.
At any moment, WSM must stay within the map, and the cell WSM is in must not have a lock.
If a cell contains both a lock with password rr and a key with password rr, then WSM can enter that cell.

There are also pp normal items and qq magic items on the map. WSM can spend steps to use normal items and magic items on the map. All items and magic items can be reused.


Normal items are very primitive: WSM can only use an item that is in the same cell as WSM.
Suppose WSM is currently at (x,y)(x,y). After using an item, WSM moves to (x,y)(x',y').

Item ID Position after moving
11 WSM moves up by one cell, i.e. (x,y)=(x1,y)(x',y')=(x-1,y)
22 WSM moves down by one cell, i.e. (x,y)=(x+1,y)(x',y')=(x+1,y)
33 WSM moves left by one cell, i.e. (x,y)=(x,y1)(x',y')=(x,y-1)
44 WSM moves right by one cell, i.e. (x,y)=(x,y+1)(x',y')=(x,y+1)

Magic items are fragile: when WSM is in the same cell as some magic item, that magic item will disappear permanently.
Magic items are powerful: WSM can use any magic item on the map.
Suppose WSM is currently at (x,y)(x,y), and the magic item is at (x0,y0)(x_0,y_0). After using the magic item, WSM moves to (x,y)(x',y').

Magic item ID Position after moving
11 x+x2=x0\frac{x+x'}{2}=x_0y+y2=y0\frac{y+y'}{2}=y_0
22 x=xx'=xy+y2=y0\frac{y+y'}{2}=y_0
33 x+x2=x0\frac{x+x'}{2}=x_0y=yy'=y

In each step, WSM can use one normal item or one magic item. What is the minimum number of steps needed to move from the cell (1,1)(1,1) to the cell (a,b)(a,b)?

Input Format

The first line contains four integers n,m,a,bn,m,a,b.
The second line contains four integers k,t,p,qk,t,p,q.
The next kk lines each contain three integers x,y,rx,y,r, meaning there is a lock with password rr at cell (x,y)(x,y).
The next tt lines each contain three integers x,y,rx,y,r, meaning there is a key with password rr at cell (x,y)(x,y).
The next pp lines each contain three integers x,y,idx,y,id, meaning there is a normal item with ID idid at cell (x,y)(x,y).
The next qq lines each contain three integers x,y,idx,y,id, meaning there is a magic item with ID idid at cell (x,y)(x,y).

Output Format

Output one integer, the minimum number of steps WSM needs. If it is impossible to reach, output -1.

2 2 2 2
0 0 8 0
1 2 4
1 1 2
2 2 1
1 1 4
2 2 4
2 1 4
1 2 3
2 1 1
2

Hint

Sample 11 Explanation

The route with the minimum number of steps is:

$\def\f#1{\xrightarrow{\bf 道具#1}} (1,1) \f{2} (2,1) \f{4} (2,2)$。

Constraints

This problem uses bundled testdata.

Note: In any cell, there may simultaneously exist multiple locks, keys, items, and magic items.

For all testdata, it is guaranteed that:

  • 1n,m4001\le n,m\le4001an1\le a\le n1bm1\le b\le m
  • 1k1031\le k \le 10^30t30\le t\le 31p5×1051\le p\le 5\times 10^50q30\le q\le 3
  • For all locks, keys, items, and magic items, 1xn1\le x\le n1ym1\le y\le m
  • For all locks, 1r1091\le r\le 10^9
  • For all keys, 1r1091\le r\le 10^9
  • For all normal items, id{1,2,3,4}id\in\{1,2,3,4\}
  • For all magic items, id{1,2,3}id\in\{1,2,3\}
Subtask ID n,mn,m\le kk\le tt\le pp\le qq\le Score
11 22 00 1313 00 1010
22 1010 ^ 300300 33
33 ^ 100100 33 ^ 2020
44 400400 00 5×1055\times10^5 00 1010
55 ^ 33 ^ 33 2525
66 10310^3 ^ ^

Translated by ChatGPT 5