Walls

시간 제한2초메모리 제한1024 MB

요약
각 칸에 두 방향 중 하나의 대각선 벽이 있고 뒤집는 비용이 주어질 때, 벽으로 둘러싸인 닫힌 영역이 생기지 않도록 하는 최소 비용을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 그리디
정답자
아직 제출이 없습니다

문제

Poor bobo is trapped in a maze!

The maze is divided into nn rows and mm columns. Each cell of the maze contains a wall across the diagonal. Thus, there are only two types of cells.

Thanks to bobo's magic power, he can change the type of cell (i,j)(i, j) with cost c_i,jc\_{i, j}. As a kind magician, bobo would like to make the maze unable to trap people anymore. That is to say, there will be no closed area surrounded by walls.

Find the minimum total cost for bobo to achieve the goal.

입력

The first line contains 22 integers n,mn, m (1≤n,m≤10001 \leq n, m \leq 1000).

Each of the following nn lines contains mm characters, which denotes the direction of wall in the cell.

Each of the last nn lines contains mm integers c_i,1,c_i,2,…,c_i,mc\_{i, 1}, c\_{i, 2}, \dots, c\_{i, m} (1≤c_i,j≤10001 \leq c\_{i, j} \leq 1000).

출력

A single number denotes the minimum of cost.

예제2

  1. 예제 1

    입력
    3 3
    /\/
    \/\
    /\/
    1 3 3
    3 1 3
    3 3 3
    
    예상 출력
    2
    
  2. 예제 2

    입력
    2 2
    \/
    /\
    1000 1000
    1000 1000
    
    예상 출력
    0