luogu#P10820. [EC Final 2020] Tube Master III
[EC Final 2020] Tube Master III
题目描述
Prof. Pang is playing Tube Master
.
The game field is divided into cells by horizontal tubes and vertical tubes. The product is an number. There are crossings of the tubes. The 2D coordinate of the crossings are (, ). We name the crossing with coordinate as crossing . We name the cell whose corners are crossings as cell for all , . Additionally, each cell contains an integer .
The above figure shows a game field with (the third sample).
Prof. Pang decides to use some of the tubes. However, the game poses several weird restrictions.
- Either or tubes connected to each crossing are used.
- There are exactly turning points adjacent to cell . A turning point is a crossing such that exactly horizontal tube and exactly vertical tube connected to it are used. A turning point is adjacent to cell if crossing is a corner of cell .
It costs to use the tube connecting crossings and . It costs to use the tube connecting crossings and . Please help Prof. Pang to find out which tubes he should use such that the restrictions are satisfied and the total cost is minimized.
输入格式
The first line contains a single positive integer denoting the number of test cases.
For each test case, the first line contains two integers , () separated by a single space.
The -th of the following lines contains integers ${count}_{i, 1}, {count}_{i, 2}, \dots, {count}_{i, m}$ () separated by single spaces.
The -th of the following lines contains integers () separated by single spaces.
The -th of the following lines contains integers () separated by single spaces.
It is guaranteed that is an number and that the total sum of over all test cases does not exceed .
输出格式
For each test case, output an integer that denotes the minimum cost.
If there is no valid configuration, output instead.
4
2 3
4 3 2
2 3 4
2 1 1
2 1 2
1 2 1
1 2 1 2
1 1 1 2
2 2
2 1
2 1
1 2
2 2
1 2
1 2 1
2 1 1
3 2
1 2
3 3
3 2
1 1
1 1
2 2
1 1
1 1 1
1 1 1
2 2 2
2 2
1 2
3 4
5 6
7 8
9 10
11 12 13
14 15 16
13
8
11
-1