結果

問題 No.421 しろくろチョコレート
ユーザー roarisroaris
提出日時 2019-11-01 14:07:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 1,401 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 78,584 KB
最終ジャッジ日時 2023-10-24 14:59:59
合計ジャッジ時間 8,255 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
55,480 KB
testcase_01 AC 130 ms
76,928 KB
testcase_02 AC 85 ms
75,972 KB
testcase_03 AC 70 ms
72,228 KB
testcase_04 AC 79 ms
75,932 KB
testcase_05 AC 85 ms
75,732 KB
testcase_06 AC 48 ms
61,292 KB
testcase_07 AC 99 ms
76,160 KB
testcase_08 AC 80 ms
75,904 KB
testcase_09 AC 82 ms
75,804 KB
testcase_10 AC 71 ms
72,952 KB
testcase_11 AC 99 ms
76,408 KB
testcase_12 AC 43 ms
55,480 KB
testcase_13 AC 43 ms
55,480 KB
testcase_14 AC 42 ms
55,480 KB
testcase_15 AC 42 ms
55,480 KB
testcase_16 AC 149 ms
77,176 KB
testcase_17 AC 196 ms
77,508 KB
testcase_18 AC 217 ms
77,572 KB
testcase_19 AC 43 ms
55,480 KB
testcase_20 AC 144 ms
77,300 KB
testcase_21 AC 144 ms
77,072 KB
testcase_22 AC 105 ms
76,588 KB
testcase_23 AC 42 ms
55,480 KB
testcase_24 AC 42 ms
55,480 KB
testcase_25 AC 42 ms
55,480 KB
testcase_26 AC 42 ms
55,480 KB
testcase_27 AC 42 ms
55,480 KB
testcase_28 AC 102 ms
76,632 KB
testcase_29 AC 123 ms
77,204 KB
testcase_30 AC 118 ms
76,868 KB
testcase_31 AC 149 ms
77,744 KB
testcase_32 AC 112 ms
76,992 KB
testcase_33 AC 123 ms
77,008 KB
testcase_34 AC 64 ms
70,328 KB
testcase_35 AC 63 ms
70,320 KB
testcase_36 AC 109 ms
76,484 KB
testcase_37 AC 227 ms
78,368 KB
testcase_38 AC 235 ms
78,468 KB
testcase_39 AC 98 ms
76,476 KB
testcase_40 AC 121 ms
77,112 KB
testcase_41 AC 93 ms
75,916 KB
testcase_42 AC 81 ms
75,684 KB
testcase_43 AC 103 ms
76,524 KB
testcase_44 AC 144 ms
77,544 KB
testcase_45 AC 84 ms
75,932 KB
testcase_46 AC 84 ms
75,744 KB
testcase_47 AC 124 ms
76,912 KB
testcase_48 AC 137 ms
77,672 KB
testcase_49 AC 95 ms
76,188 KB
testcase_50 AC 85 ms
75,960 KB
testcase_51 AC 149 ms
77,524 KB
testcase_52 AC 112 ms
76,508 KB
testcase_53 AC 77 ms
75,916 KB
testcase_54 AC 85 ms
75,968 KB
testcase_55 AC 69 ms
72,196 KB
testcase_56 AC 71 ms
70,232 KB
testcase_57 AC 83 ms
75,724 KB
testcase_58 AC 132 ms
76,800 KB
testcase_59 AC 86 ms
75,892 KB
testcase_60 AC 250 ms
78,584 KB
testcase_61 AC 162 ms
77,652 KB
testcase_62 AC 44 ms
55,480 KB
testcase_63 AC 44 ms
55,480 KB
testcase_64 AC 47 ms
61,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
from collections import defaultdict

def dfs(v, t, f, used):
    if v == t:
        return f
    
    used[v] = True
    
    for nn, cap in edges[v].items():
        if not used[nn] and cap > 0:
            d = dfs(nn, t, min(f, cap), used)
            
            if d > 0:
                edges[v][nn] -= d
                edges[nn][v] += d
                
                return d
    
    return 0
    
def max_flow(s, t):
    flow = 0
    
    while True:
        used = [False] * (N*M+2)
        f = dfs(s, t, 10**18, used)
        
        if f == 0:
            return flow
            
        flow += f

N, M = map(int, input().split())
S = [input() for _ in range(N)]
edges = defaultdict(dict)
wc, bc = 0, 0

for i in range(N):
    for j in range(M):
        if S[i][j] == 'w':
            wc += 1
            edges[0][M*i+j+1] = 1
            edges[M*i+j+1][0] = 0
            
            for ni, nj in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                if 0<=ni<N and 0<=nj<M and S[ni][nj] != '.':
                    edges[M*i+j+1][M*ni+nj+1] = 1
                    edges[M*ni+nj+1][M*i+j+1] = 0
        
        if S[i][j] == 'b':
            bc += 1
            edges[M*i+j+1][N*M+1] = 1
            edges[N*M+1][M*i+j+1] = 0

max_pair = max_flow(0, N*M+1)
wc -= max_pair
bc -= max_pair

print(max_pair*100+min(wc, bc)*10+abs(wc-bc))
0