結果

問題 No.421 しろくろチョコレート
ユーザー rlangevinrlangevin
提出日時 2023-07-20 12:24:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 2,388 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 79,024 KB
最終ジャッジ日時 2023-10-20 13:25:30
合計ジャッジ時間 8,772 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,588 KB
testcase_01 AC 139 ms
78,060 KB
testcase_02 AC 69 ms
70,520 KB
testcase_03 AC 53 ms
63,816 KB
testcase_04 AC 61 ms
68,340 KB
testcase_05 AC 72 ms
72,232 KB
testcase_06 AC 50 ms
61,108 KB
testcase_07 AC 109 ms
76,812 KB
testcase_08 AC 63 ms
68,436 KB
testcase_09 AC 69 ms
70,296 KB
testcase_10 AC 67 ms
70,376 KB
testcase_11 AC 114 ms
76,884 KB
testcase_12 AC 42 ms
55,588 KB
testcase_13 AC 44 ms
55,588 KB
testcase_14 AC 42 ms
55,588 KB
testcase_15 AC 42 ms
55,588 KB
testcase_16 AC 138 ms
78,432 KB
testcase_17 AC 140 ms
78,764 KB
testcase_18 AC 136 ms
78,476 KB
testcase_19 AC 42 ms
55,588 KB
testcase_20 AC 124 ms
78,288 KB
testcase_21 AC 155 ms
78,332 KB
testcase_22 AC 120 ms
77,496 KB
testcase_23 AC 41 ms
55,588 KB
testcase_24 AC 42 ms
55,588 KB
testcase_25 AC 42 ms
55,588 KB
testcase_26 AC 41 ms
55,588 KB
testcase_27 AC 42 ms
55,588 KB
testcase_28 AC 104 ms
77,228 KB
testcase_29 AC 117 ms
78,100 KB
testcase_30 AC 115 ms
77,900 KB
testcase_31 AC 124 ms
78,896 KB
testcase_32 AC 130 ms
78,476 KB
testcase_33 AC 121 ms
77,984 KB
testcase_34 AC 49 ms
61,540 KB
testcase_35 AC 53 ms
64,200 KB
testcase_36 AC 97 ms
77,008 KB
testcase_37 AC 138 ms
78,372 KB
testcase_38 AC 161 ms
79,004 KB
testcase_39 AC 88 ms
76,748 KB
testcase_40 AC 134 ms
77,928 KB
testcase_41 AC 71 ms
72,436 KB
testcase_42 AC 60 ms
67,960 KB
testcase_43 AC 95 ms
76,948 KB
testcase_44 AC 154 ms
78,704 KB
testcase_45 AC 68 ms
70,624 KB
testcase_46 AC 61 ms
67,972 KB
testcase_47 AC 119 ms
77,308 KB
testcase_48 AC 148 ms
78,748 KB
testcase_49 AC 95 ms
76,952 KB
testcase_50 AC 68 ms
72,668 KB
testcase_51 AC 138 ms
78,308 KB
testcase_52 AC 96 ms
76,724 KB
testcase_53 AC 59 ms
66,368 KB
testcase_54 AC 66 ms
70,460 KB
testcase_55 AC 66 ms
70,296 KB
testcase_56 AC 62 ms
68,096 KB
testcase_57 AC 65 ms
70,152 KB
testcase_58 AC 126 ms
77,992 KB
testcase_59 AC 89 ms
76,528 KB
testcase_60 AC 174 ms
79,024 KB
testcase_61 AC 149 ms
78,660 KB
testcase_62 AC 43 ms
55,588 KB
testcase_63 AC 42 ms
55,588 KB
testcase_64 AC 47 ms
61,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
class Maxflow:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        
    def add_edge(self, u, v, c):
        self.G[u].append([v, c, len(self.G[v])])
        self.G[v].append([u, 0, len(self.G[u]) - 1])
        
    def bfs(self, s):
        D = [-1] * self.N
        D[s] = 0
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for next, cap, _ in self.G[u]:
                if cap > 0 and D[next] < 0:
                    D[next] = D[u] + 1
                    Q.append(next)
        return D
    
    def dfs(self, v, t, f, removed, D):
        if v == t:
            return f
        
        while removed[v] < len(self.G[v]):
            next, cap, rev = self.G[v][removed[v]]
            if cap > 0 and D[v] < D[next]:
                flow = self.dfs(next, t, min(f, cap), removed, D)
                if flow > 0:
                    self.G[v][removed[v]][1] -= flow
                    self.G[next][rev][1] += flow
                    return flow
                
            removed[v] += 1
        return 0
    
    def calc_max_flow(self, s, t):
        flow = 0
        while True:
            D = self.bfs(s)
            if D[t] < 0:
                return flow
            removed = [0] * self.N
            while True:
                f = self.dfs(s, t, 1e10, removed, D)
                if f == 0:
                    break
                flow += f


N, M = map(int, input().split())
S = []
w, b = 0, 0
for i in range(N):
    S.append(list(input()))
    w += S[i].count("w")
    b += S[i].count("b")

start = N * M
goal = start + 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
G = Maxflow(N * M + 2)
for i in range(N):
    for j in range(M):
        if S[i][j] == ".":
            continue
        if (i + j) % 2 == 0:
            G.add_edge(i * M + j, goal, 1)
            continue

        G.add_edge(start, i * M + j, 1)
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > N - 1 or y < 0 or y > M - 1:
                continue
            if S[x][y] == ".":
                continue
            if S[i][j] != S[x][y]:
                G.add_edge(i * M + j, x * M + y, 1)

ans = 0
mf = G.calc_max_flow(start, goal)
ans += mf * 100
b, w = b - mf, w - mf
ans += min(b, w) * 10 + max(b, w) - min(b, w)
print(ans)
0