結果

問題 No.421 しろくろチョコレート
ユーザー ayaoniayaoni
提出日時 2020-09-25 20:09:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 2,585 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 81,816 KB
実行使用メモリ 79,664 KB
最終ジャッジ日時 2023-10-24 15:03:43
合計ジャッジ時間 7,304 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,484 KB
testcase_01 AC 118 ms
77,508 KB
testcase_02 AC 60 ms
66,216 KB
testcase_03 AC 54 ms
63,548 KB
testcase_04 AC 60 ms
66,212 KB
testcase_05 AC 68 ms
70,208 KB
testcase_06 AC 54 ms
61,356 KB
testcase_07 AC 101 ms
76,896 KB
testcase_08 AC 66 ms
70,456 KB
testcase_09 AC 64 ms
67,988 KB
testcase_10 AC 60 ms
65,832 KB
testcase_11 AC 98 ms
77,056 KB
testcase_12 AC 44 ms
55,484 KB
testcase_13 AC 43 ms
55,484 KB
testcase_14 AC 43 ms
55,488 KB
testcase_15 AC 42 ms
55,484 KB
testcase_16 AC 141 ms
78,984 KB
testcase_17 AC 137 ms
78,844 KB
testcase_18 AC 146 ms
78,856 KB
testcase_19 AC 42 ms
55,484 KB
testcase_20 AC 118 ms
77,684 KB
testcase_21 AC 140 ms
78,652 KB
testcase_22 AC 98 ms
77,104 KB
testcase_23 AC 43 ms
55,484 KB
testcase_24 AC 43 ms
55,484 KB
testcase_25 AC 44 ms
55,484 KB
testcase_26 AC 43 ms
55,484 KB
testcase_27 AC 43 ms
55,484 KB
testcase_28 AC 79 ms
74,108 KB
testcase_29 AC 99 ms
77,720 KB
testcase_30 AC 111 ms
77,556 KB
testcase_31 AC 102 ms
78,632 KB
testcase_32 AC 106 ms
77,740 KB
testcase_33 AC 121 ms
78,140 KB
testcase_34 AC 49 ms
63,596 KB
testcase_35 AC 53 ms
64,052 KB
testcase_36 AC 105 ms
77,164 KB
testcase_37 AC 143 ms
78,816 KB
testcase_38 AC 154 ms
78,992 KB
testcase_39 AC 75 ms
72,904 KB
testcase_40 AC 111 ms
77,648 KB
testcase_41 AC 67 ms
70,144 KB
testcase_42 AC 68 ms
70,140 KB
testcase_43 AC 88 ms
77,020 KB
testcase_44 AC 124 ms
77,776 KB
testcase_45 AC 60 ms
66,224 KB
testcase_46 AC 68 ms
70,140 KB
testcase_47 AC 111 ms
77,428 KB
testcase_48 AC 123 ms
78,976 KB
testcase_49 AC 79 ms
73,240 KB
testcase_50 AC 65 ms
68,268 KB
testcase_51 AC 127 ms
77,824 KB
testcase_52 AC 101 ms
77,128 KB
testcase_53 AC 64 ms
66,320 KB
testcase_54 AC 67 ms
70,380 KB
testcase_55 AC 62 ms
68,184 KB
testcase_56 AC 59 ms
65,832 KB
testcase_57 AC 64 ms
68,064 KB
testcase_58 AC 111 ms
77,428 KB
testcase_59 AC 81 ms
73,396 KB
testcase_60 AC 171 ms
79,664 KB
testcase_61 AC 152 ms
79,040 KB
testcase_62 AC 43 ms
55,492 KB
testcase_63 AC 43 ms
55,492 KB
testcase_64 AC 47 ms
61,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


class Dinic:
    def __init__(self, N, inf):
        self.N = N
        self.inf = inf
        self.G = [[] for _ in range(N)]
        self.level = [0]*N

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def bfs(self, s):
        self.level = [-1]*self.N
        deq = deque([s])
        self.level[s] = 0
        while deq:
            v = deq.pop()
            lv = self.level[v] + 1
            for w, cap, _ in self.G[v]:
                if cap > 0 and self.level[w] == -1:
                    self.level[w] = lv
                    deq.appendleft(w)

    def dfs(self, v, t, f):
        if v == t:
            return f
        for e in self.iter[v]:
            w, cap, rev = e
            if cap > 0 and self.level[v] < self.level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d > 0:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t] == -1:
                return flow
            *self.iter, = map(iter, self.G)
            f = self.inf
            while f > 0:
                f = self.dfs(s, t, self.inf)
                flow += f


import sys

sys.setrecursionlimit(10**7)
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N,M = MI()
S = [LS2() for _ in range(N)]

Di = Dinic(N*M+2,10000)
s,t = N*M,N*M+1

white,black = 0,0
for i in range(N):
    for j in range(M):
        if S[i][j] == '.':
            continue
        a = M*i+j
        if (i+j) % 2 == 0:
            white += 1
            Di.add_edge(s,a,1)
            if 0 <= i-1 and S[i-1][j] != '.':
                Di.add_edge(a,M*(i-1)+j,1)
            if i+1 < N and S[i+1][j] != '.':
                Di.add_edge(a,M*(i+1)+j,1)
            if 0 <= j-1 and S[i][j-1] != '.':
                Di.add_edge(a,M*i+j-1,1)
            if j+1 < M and S[i][j+1] != '.':
                Di.add_edge(a,M*i+j+1,1)
        else:
            black += 1
            Di.add_edge(a,t,1)


flow = Di.flow(s,t)
print(100*flow+10*(min(white,black)-flow)+(abs(white-black)))
0