結果

問題 No.421 しろくろチョコレート
ユーザー 👑 rin204rin204
提出日時 2022-07-12 19:00:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 2,411 bytes
コンパイル時間 519 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 80,600 KB
最終ジャッジ日時 2023-09-05 21:33:23
合計ジャッジ時間 12,264 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,024 KB
testcase_01 AC 191 ms
79,568 KB
testcase_02 AC 142 ms
78,308 KB
testcase_03 AC 111 ms
76,196 KB
testcase_04 AC 127 ms
77,372 KB
testcase_05 AC 129 ms
76,540 KB
testcase_06 AC 112 ms
76,652 KB
testcase_07 AC 151 ms
78,768 KB
testcase_08 AC 133 ms
76,944 KB
testcase_09 AC 131 ms
76,508 KB
testcase_10 AC 122 ms
76,764 KB
testcase_11 AC 162 ms
78,544 KB
testcase_12 AC 98 ms
71,056 KB
testcase_13 AC 93 ms
71,156 KB
testcase_14 AC 93 ms
71,060 KB
testcase_15 AC 92 ms
70,688 KB
testcase_16 AC 176 ms
79,572 KB
testcase_17 AC 196 ms
80,156 KB
testcase_18 AC 189 ms
80,460 KB
testcase_19 AC 94 ms
71,172 KB
testcase_20 AC 171 ms
79,172 KB
testcase_21 AC 186 ms
79,496 KB
testcase_22 AC 149 ms
79,040 KB
testcase_23 AC 93 ms
71,000 KB
testcase_24 AC 95 ms
71,112 KB
testcase_25 AC 94 ms
70,976 KB
testcase_26 AC 99 ms
71,068 KB
testcase_27 AC 94 ms
70,576 KB
testcase_28 AC 160 ms
79,028 KB
testcase_29 AC 166 ms
79,088 KB
testcase_30 AC 180 ms
79,476 KB
testcase_31 AC 165 ms
79,820 KB
testcase_32 AC 181 ms
80,580 KB
testcase_33 AC 172 ms
78,788 KB
testcase_34 AC 117 ms
76,960 KB
testcase_35 AC 118 ms
76,628 KB
testcase_36 AC 164 ms
78,492 KB
testcase_37 AC 197 ms
79,840 KB
testcase_38 AC 204 ms
79,916 KB
testcase_39 AC 154 ms
79,000 KB
testcase_40 AC 185 ms
79,652 KB
testcase_41 AC 134 ms
76,836 KB
testcase_42 AC 130 ms
76,808 KB
testcase_43 AC 160 ms
77,996 KB
testcase_44 AC 189 ms
79,492 KB
testcase_45 AC 141 ms
77,740 KB
testcase_46 AC 137 ms
77,704 KB
testcase_47 AC 170 ms
78,672 KB
testcase_48 AC 180 ms
80,068 KB
testcase_49 AC 149 ms
78,764 KB
testcase_50 AC 125 ms
77,260 KB
testcase_51 AC 175 ms
79,504 KB
testcase_52 AC 154 ms
78,228 KB
testcase_53 AC 132 ms
77,656 KB
testcase_54 AC 143 ms
77,620 KB
testcase_55 AC 124 ms
76,956 KB
testcase_56 AC 117 ms
76,936 KB
testcase_57 AC 127 ms
76,844 KB
testcase_58 AC 183 ms
79,756 KB
testcase_59 AC 138 ms
77,176 KB
testcase_60 AC 212 ms
80,600 KB
testcase_61 AC 200 ms
79,940 KB
testcase_62 AC 97 ms
70,684 KB
testcase_63 AC 98 ms
70,956 KB
testcase_64 AC 102 ms
75,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])
 
    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth
 
    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))

n, m = map(int, input().split())
S = [input() for _ in range(n)]

directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
G = Dinic(n * m + 2)
s = n * m
t = s + 1
f = lambda i, j: i * m + j
b = 0
w = 0
for i in range(n):
    for j in range(m):
        if S[i][j] == ".":
            continue
        p = f(i, j)
        if S[i][j] == "w":
            w += 1
            G.add_link(s, p, 1)
            for di, dj in directions:
                ni = i + di
                nj = j + dj
                if ni == -1 or ni == n or nj == -1 or nj == m:
                    continue
                G.add_link(p, f(ni, nj), 1)
        else:
            b += 1
            G.add_link(p, t, 1)

fl = G.max_flow(s, t)
ans = fl * 100
b -= fl
w -= fl
ans += min(b, w) * 10
ans += max(b, w) - min(b, w)
print(ans)
0