結果

問題 No.421 しろくろチョコレート
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-02 06:34:15
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 1,265 ms
コンパイル使用メモリ 81,736 KB
実行使用メモリ 78,880 KB
最終ジャッジ日時 2023-10-24 15:04:07
合計ジャッジ時間 7,688 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,500 KB
testcase_01 AC 128 ms
77,744 KB
testcase_02 AC 71 ms
72,628 KB
testcase_03 AC 55 ms
63,760 KB
testcase_04 AC 67 ms
70,540 KB
testcase_05 AC 71 ms
72,252 KB
testcase_06 AC 50 ms
61,384 KB
testcase_07 AC 98 ms
76,736 KB
testcase_08 AC 69 ms
70,384 KB
testcase_09 AC 64 ms
67,956 KB
testcase_10 AC 64 ms
68,284 KB
testcase_11 AC 100 ms
76,808 KB
testcase_12 AC 43 ms
55,500 KB
testcase_13 AC 43 ms
55,500 KB
testcase_14 AC 43 ms
55,500 KB
testcase_15 AC 42 ms
55,500 KB
testcase_16 AC 142 ms
78,508 KB
testcase_17 AC 140 ms
78,324 KB
testcase_18 AC 136 ms
78,348 KB
testcase_19 AC 42 ms
55,500 KB
testcase_20 AC 124 ms
77,996 KB
testcase_21 AC 124 ms
78,136 KB
testcase_22 AC 106 ms
76,948 KB
testcase_23 AC 43 ms
55,500 KB
testcase_24 AC 42 ms
55,500 KB
testcase_25 AC 42 ms
55,500 KB
testcase_26 AC 43 ms
55,500 KB
testcase_27 AC 42 ms
55,500 KB
testcase_28 AC 98 ms
76,968 KB
testcase_29 AC 111 ms
77,360 KB
testcase_30 AC 124 ms
77,728 KB
testcase_31 AC 111 ms
78,816 KB
testcase_32 AC 117 ms
77,268 KB
testcase_33 AC 135 ms
78,116 KB
testcase_34 AC 55 ms
63,812 KB
testcase_35 AC 60 ms
65,940 KB
testcase_36 AC 110 ms
76,808 KB
testcase_37 AC 137 ms
78,320 KB
testcase_38 AC 145 ms
78,672 KB
testcase_39 AC 94 ms
76,744 KB
testcase_40 AC 123 ms
77,964 KB
testcase_41 AC 70 ms
70,256 KB
testcase_42 AC 72 ms
72,340 KB
testcase_43 AC 95 ms
76,748 KB
testcase_44 AC 125 ms
78,200 KB
testcase_45 AC 68 ms
70,552 KB
testcase_46 AC 72 ms
72,264 KB
testcase_47 AC 118 ms
77,020 KB
testcase_48 AC 117 ms
78,512 KB
testcase_49 AC 99 ms
76,800 KB
testcase_50 AC 69 ms
70,376 KB
testcase_51 AC 144 ms
78,324 KB
testcase_52 AC 105 ms
76,928 KB
testcase_53 AC 67 ms
70,640 KB
testcase_54 AC 74 ms
72,564 KB
testcase_55 AC 64 ms
68,268 KB
testcase_56 AC 64 ms
67,912 KB
testcase_57 AC 71 ms
72,484 KB
testcase_58 AC 123 ms
77,776 KB
testcase_59 AC 86 ms
76,212 KB
testcase_60 AC 163 ms
78,880 KB
testcase_61 AC 144 ms
78,592 KB
testcase_62 AC 43 ms
55,500 KB
testcase_63 AC 42 ms
55,500 KB
testcase_64 AC 51 ms
63,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Dinic:
    def __init__(self, n, INF=float('inf')):
        self.n = n
        self.INF = INF
        self.pos = []
        self.G = [[] for _ in range(n)]
        self.level = None
        self.iters = None

    def add_edge(self, fr, to, cap):
        self.pos.append((fr, len(self.G[fr])))
        self.G[fr].append([cap, to, len(self.G[to])])
        self.G[to].append([0, fr, len(self.G[fr])-1])

    def bfs(self, s):
        level = [-1]*self.n
        level[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.G[v]:
                if cap > 0 and level[to] < 0:
                    level[to] = level[v]+1
                    q.append(to)
        self.level = level

    def dfs(self, v, t, flow):
        if v == t:
            return flow
        for i in range(self.iters[v], len(self.G[v])):
            self.iters[v] = i
            cap, to, rev = self.G[v][i]
            if cap > 0 and self.level[v] < self.level[to]:
                d = self.dfs(to, t, min(flow, cap))
                if d > 0:
                    self.G[v][i][0] -= d
                    self.G[to][rev][0] += d
                    return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t] < 0:
                return flow
            self.iters = [0]*self.n
            f = self.dfs(s, t, self.INF)
            while f > 0:
                flow += f
                f = self.dfs(s, t, self.INF)

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

INF = 10**9
dinic = Dinic(n*m+2, INF)

w, b = 0, 0
s = n*m
t = n*m+1
for i in range(n):
    for j in range(m):
        if (i+j)%2 == 0 and S[i][j] != '.':
            w += 1
            dinic.add_edge(s, i*m+j, 1) # source > even
        if (i+j)%2 == 1 and S[i][j] != '.':
            b += 1
            dinic.add_edge(i*m+j, t, 1) # odd > sink

for i, row in enumerate(S):
    for j, c in enumerate(row):
        v = i*m+j
        if c == '.' or (i+j)%2 == 1:
            continue
        for di, dj in (-1, 0), (1, 0), (0, -1), (0, 1):
            ni, nj = i+di, j+dj
            if 0 <= ni < n and 0 <= nj < m:
                u = ni*m+nj
                if S[ni][nj] != '.':
                    dinic.add_edge(v, u, 1) # even > odd

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