結果

問題 No.421 しろくろチョコレート
ユーザー maspymaspy
提出日時 2020-03-18 02:24:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 4,027 bytes
コンパイル時間 597 ms
コンパイル使用メモリ 12,360 KB
実行使用メモリ 12,684 KB
最終ジャッジ日時 2023-10-24 15:00:28
合計ジャッジ時間 4,209 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,536 KB
testcase_01 AC 45 ms
11,380 KB
testcase_02 AC 35 ms
10,888 KB
testcase_03 AC 30 ms
10,580 KB
testcase_04 AC 33 ms
10,768 KB
testcase_05 AC 34 ms
10,740 KB
testcase_06 AC 32 ms
10,592 KB
testcase_07 AC 39 ms
11,032 KB
testcase_08 AC 36 ms
10,904 KB
testcase_09 AC 32 ms
10,644 KB
testcase_10 AC 33 ms
10,644 KB
testcase_11 AC 43 ms
11,072 KB
testcase_12 AC 31 ms
10,532 KB
testcase_13 AC 31 ms
10,532 KB
testcase_14 AC 30 ms
10,532 KB
testcase_15 AC 30 ms
10,532 KB
testcase_16 AC 64 ms
11,868 KB
testcase_17 AC 55 ms
11,896 KB
testcase_18 AC 60 ms
11,928 KB
testcase_19 AC 31 ms
10,532 KB
testcase_20 AC 53 ms
11,512 KB
testcase_21 AC 56 ms
11,684 KB
testcase_22 AC 42 ms
11,092 KB
testcase_23 AC 30 ms
10,532 KB
testcase_24 AC 31 ms
10,532 KB
testcase_25 AC 30 ms
10,532 KB
testcase_26 AC 30 ms
10,532 KB
testcase_27 AC 30 ms
10,532 KB
testcase_28 AC 39 ms
11,408 KB
testcase_29 AC 40 ms
11,588 KB
testcase_30 AC 40 ms
11,432 KB
testcase_31 AC 48 ms
12,684 KB
testcase_32 AC 42 ms
11,484 KB
testcase_33 AC 44 ms
11,496 KB
testcase_34 AC 34 ms
10,848 KB
testcase_35 AC 34 ms
10,856 KB
testcase_36 AC 39 ms
11,096 KB
testcase_37 AC 57 ms
11,860 KB
testcase_38 AC 75 ms
12,128 KB
testcase_39 AC 38 ms
11,016 KB
testcase_40 AC 47 ms
11,352 KB
testcase_41 AC 33 ms
10,756 KB
testcase_42 AC 32 ms
10,684 KB
testcase_43 AC 37 ms
11,068 KB
testcase_44 AC 58 ms
11,724 KB
testcase_45 AC 34 ms
10,764 KB
testcase_46 AC 33 ms
10,736 KB
testcase_47 AC 41 ms
11,272 KB
testcase_48 AC 45 ms
12,164 KB
testcase_49 AC 37 ms
11,000 KB
testcase_50 AC 35 ms
10,832 KB
testcase_51 AC 55 ms
11,692 KB
testcase_52 AC 38 ms
11,032 KB
testcase_53 AC 33 ms
10,704 KB
testcase_54 AC 36 ms
10,948 KB
testcase_55 AC 32 ms
10,652 KB
testcase_56 AC 33 ms
10,648 KB
testcase_57 AC 34 ms
10,736 KB
testcase_58 AC 44 ms
11,348 KB
testcase_59 AC 36 ms
10,832 KB
testcase_60 AC 73 ms
12,260 KB
testcase_61 AC 81 ms
12,308 KB
testcase_62 AC 31 ms
10,540 KB
testcase_63 AC 30 ms
10,540 KB
testcase_64 AC 34 ms
10,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3.8
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque


# %%
class Dinic:
    def __init__(self, N, source, sink):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.source = source
        self.sink = sink

    def add_edge(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, 0, n1])  # 逆辺を cap 0 で追加

    def add_edge_undirected(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, cap, n1])

    def bfs(self):
        level = [0] * self.N
        G = self.G
        source = self.source
        sink = self.sink
        q = deque([source])
        level[source] = 1
        pop = q.popleft
        append = q.append
        while q:
            v = pop()
            lv = level[v] + 1
            for to, cap, rev in G[v]:
                if not cap:
                    continue
                if level[to]:
                    continue
                level[to] = lv
                if to == sink:
                    self.level = level
                    return
                append(to)
        self.level = level

    def dfs(self):
        INF = 10**18
        G = self.G
        sink = self.sink
        prog = self.progress
        level = self.level
        ascend = False
        ff = 0
        stack = [(self.source, INF)]
        while stack:
            if ff:
                # このまま更新だけして戻っていく
                v, f = stack.pop()
                p = prog[v]
                to, cap, rev = G[v][p]
                G[v][p][1] -= ff
                G[to][rev][1] += ff
                continue
            v, f = stack[-1]
            if v == sink:
                ff = f
                stack.pop()
                continue
            E = G[v]
            lv = level[v]
            if ascend:
                # 流せずに戻ってきた
                prog[v] += 1
            find_flag = False
            for i in range(prog[v], len(E)):
                to, cap, rev = E[i]
                prog[v] = i
                if not cap:
                    continue
                if level[to] <= lv:
                    continue
                find_flag = True
                break
            if not find_flag:
                ascend = True
                stack.pop()
                continue
            ascend = False
            x = f if f < cap else cap
            stack.append((to, x))
        return ff

    def max_flow(self):
        flow = 0
        while True:
            self.bfs()
            if not self.level[self.sink]:
                return flow
            self.progress = [0] * self.N
            while True:
                f = self.dfs()
                if not f:
                    break
                flow += f
        return flow


# %%
H, W = map(int, readline().split())
S = list(''.join(read().decode().split()))


# %%
source = H * W
sink = H * W + 1
dinic = Dinic(N=H * W + 2, source=source, sink=sink)
for h in range(H - 1):
    for w in range(W):
        i = W * h + w
        j = i + W
        if S[i] == 'b' and S[j] == 'w':
            dinic.add_edge(i, j, 1)
        elif S[i] == 'w' and S[j] == 'b':
            dinic.add_edge(j, i, 1)
for h in range(H):
    for w in range(W - 1):
        i = W * h + w
        j = i + 1
        if S[i] == 'b' and S[j] == 'w':
            dinic.add_edge(i, j, 1)
        elif S[i] == 'w' and S[j] == 'b':
            dinic.add_edge(j, i, 1)
for i in range(H * W):
    if S[i] == 'b':
        dinic.add_edge(source, i, 1)
    elif S[i] == 'w':
        dinic.add_edge(i, sink, 1)


# %%
f = dinic.max_flow()

# %%
B = S.count('b')
W = S.count('w')
score = 100 * f
B -= f
W -= f
x = min(B, W)
score += 10 * x
B -= x
W -= x
score += B + W
print(score)
0