#P4142. 洞穴遇险

洞穴遇险

Description

{{The entire cave is an NNN*N grid. Each cell is denoted as (X,Y)(X, Y), where 1X,YN1 \le X, Y \le N. Here XX is the row index from top to bottom, and YY is the column index from left to right. (1,1)(1,1) is the top-left corner, (1,N)(1,N) is the top-right corner, (N,1)(N,1) is the bottom-left corner, and (N,N)(N,N) is the bottom-right corner.

Cells with X+YX+Y odd have an instability VX,YV_{X,Y}, while cells with X+YX+Y even have instability 00.

ZRQ happens to have MM pillars that can support the cave. The strength of each pillar can be regarded as infinite.

As long as a cell is supported, its instability becomes 00.

Each pillar is L-shaped: in addition to occupying its current cell (the corner), it must also occupy two adjacent cells so that the three cells form an L shape. You can place it in any of the 44 orientations.

Occupying adjacent cells with a pillar does not reduce their instability (in other words, a pillar only has force at its corner cell).

Some cells have already collapsed, so you cannot place a pillar there, and these cells cannot be occupied either. There are KK such cells. Their instability is 00 (even if X+YX+Y is odd, a collapsed cell’s instability is still 00).

ZRQ asks: after placing some pillars (you do not have to use all MM pillars), what is the minimum possible sum of instabilities?}}

Input Format

{{The first line contains three integers N,M,KN, M, K.

The next NN lines each contain NN integers, representing the instability of each cell. It is guaranteed that cells with X+YX+Y even and collapsed cells have instability 00.

The next KK lines each contain two integers X,YX, Y, denoting the coordinates of the collapsed cells.}}

Output Format

{{Output a single integer: the minimum possible sum of instabilities.}}

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

Hint

{{There are 1010 test points, each worth 1010 points, for a total of 100100 points.

Constraints:

  • For test points 1133: 1N61 \le N \le 6.
  • For test points 4477: 1N111 \le N \le 11.
  • For test points 881010: 1N501 \le N \le 50.
  • For all test points: 0MN230 \le M \le \frac{N^2}{3}, 0KN20 \le K \le N^2, 0VX,Y1060 \le V_{X,Y} \le 10^6.

Sample #1 explanation: It is clearly impossible to have any two unstable cells both covered by a pillar’s corner. Therefore, just cover (2,1)(2,1) with a pillar’s corner. The remaining instability is V1,2+V2,3+V3,2=1+1+1=3V_{1,2}+V_{2,3}+V_{3,2}=1+1+1=3.

Sample #2 explanation: None can be placed. The remaining instability is V1,2+V2,3+V3,2=2+4+3=9V_{1,2}+V_{2,3}+V_{3,2}=2+4+3=9.}}

Translated by ChatGPT 5