#P3159. [CQOI2012] 交换棋子

    ID: 2208 远端评测题 1000ms 125MiB 尝试: 3 已通过: 0 难度: 8 上传者: 标签>2012重庆各省省选网络流费用流

[CQOI2012] 交换棋子

Description

There is a black-and-white board with nn rows and mm columns. Each time, you may swap the pieces in two adjacent cells (adjacent means sharing an edge or a vertex) to reach a target state. The cell at row ii, column jj may participate in at most mi,jm_{i,j} swaps.

Input Format

The first line contains two integers n,mn, m.
The next nn lines describe the initial state; each line is a binary string of length mm, where 00 denotes a black piece and 11 denotes a white piece.
The next nn lines describe the target state in the same format as the initial state.
The next nn lines each contain a string of mm digits from '0' to '9', representing the upper bound on the number of times each cell may participate in swaps.

Output Format

Output a single line with the minimum total number of swaps. If there is no solution, output 1-1.

3 3
110
000
001
000
110
100
222
222
222
4

Hint

Constraints

For 100%100\% of the testdata, 1n,m201 \le n, m \le 20.

Translated by ChatGPT 5