Logo
(追記) (追記ここまで)

19395번 - Tube Master II 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB114337.500%

문제

Yuuka is playing "Tube Master". The game field is divided into $n \times m$ cells and $(n + 1) \times (m + 1)$ crossings connected by $(n + 1) \times m$ horizontal tubes and $n \times (m + 1)$ vertical ones. The cells are conveniently labeled with $(i, j)$ for 1ドル \leq i \leq n,ドル 1ドル \leq j \leq m,ドル and the crossings are labeled with $(i, j)$ for 1ドル \leq i \leq (n+1),ドル 1ドル \leq j \leq (m+1)$. Additionally, each cell $(i, j)$ contains an integer $\mathit{count}_{i, j}$.

Yuuka decides to use some of the tubes. However, the game poses several weird restrictions.

  1. Either 0ドル$ or 2ドル$ tubes connected to each crossing are used.
  2. No two consecutive horizontal tubes are used simultaneously, and no consecutive vertical tubes are used simultaneously. Two tubes are consecutive if and only if they share the same crossing.
  3. Exactly $\mathit{count}_{i, j}$ tubes surrounding cell $(i, j)$ are used.

Using the tube connecting crossing $(i, j)$ and $(i, j + 1)$ costs $a_{i, j},ドル and using the tube connecting crossing $(i, j)$ and $(i + 1, j)$ costs $b_{i, j}$. Yuuka would like to find a configuration satisfying the above constrains with the minimum possible total cost.

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains two integers $n$ and $m$ (1ドル \leq n, m \leq 100$).

The $i$-th of the following $n$ lines contains $m$ integers $\mathit{count}_{i, 1}, \mathit{count}_{i, 2}, \dots, \mathit{count}_{i, m}$ (0ドル \leq \mathit{count}_{i, j} \leq 4$).

The $i$-th of the next $(n + 1)$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \dots, a_{i, m}$.

The $i$-th of the last $n$ lines contains $(m + 1)$ integers $b_{i, 1}, b_{i, 2}, \dots, b_{i, m + 1}$.

The constraints are: 1ドル \leq a_{i, j}, b_{i, j} \leq 10^9$.

It is guaranteed that the total sum of $n \cdot m$ in all test cases does not exceed 10ドル^4$.

출력

For each test case, output an integer which denotes the minimum cost of the configuration. If there is no valid configuration, output "-1" instead.

제한

예제 입력 1

1 3
4 2 4
1 1 1
1 1 1
1 1 1 1
1 2
3 3
1 1
1 1
1 1 1
3 3
2 3 2
3 0 3
2 3 2
1 2 3
4 5 6
7 8 9
11 12 13
1 2 3 4
5 6 7 8
9 10 11 12

예제 출력 1

8
-1
79

힌트

출처

Camp > Petrozavodsk Programming Camp > Winter 2017 > Day 5: Tsinghua U Deep Dark Fantasy Contest D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /