結果

問題 No.421 しろくろチョコレート
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-09-10 12:55:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 16,348 KB
最終ジャッジ日時 2024-09-23 07:08:13
合計ジャッジ時間 8,869 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,136 KB
testcase_01 AC 154 ms
12,672 KB
testcase_02 AC 60 ms
11,776 KB
testcase_03 AC 35 ms
11,392 KB
testcase_04 AC 47 ms
11,520 KB
testcase_05 AC 49 ms
11,648 KB
testcase_06 AC 36 ms
11,136 KB
testcase_07 AC 82 ms
12,032 KB
testcase_08 AC 54 ms
11,904 KB
testcase_09 AC 40 ms
11,392 KB
testcase_10 AC 42 ms
11,264 KB
testcase_11 AC 88 ms
12,288 KB
testcase_12 AC 32 ms
11,008 KB
testcase_13 AC 32 ms
11,008 KB
testcase_14 AC 32 ms
11,008 KB
testcase_15 AC 32 ms
11,136 KB
testcase_16 AC 262 ms
13,696 KB
testcase_17 AC 342 ms
13,696 KB
testcase_18 AC 337 ms
13,696 KB
testcase_19 AC 32 ms
11,136 KB
testcase_20 AC 184 ms
13,184 KB
testcase_21 AC 217 ms
13,420 KB
testcase_22 AC 85 ms
12,288 KB
testcase_23 AC 32 ms
10,880 KB
testcase_24 AC 31 ms
11,008 KB
testcase_25 AC 32 ms
10,880 KB
testcase_26 AC 32 ms
11,136 KB
testcase_27 AC 31 ms
11,008 KB
testcase_28 AC 113 ms
12,544 KB
testcase_29 AC 157 ms
13,184 KB
testcase_30 AC 137 ms
12,544 KB
testcase_31 AC 454 ms
16,348 KB
testcase_32 AC 133 ms
12,928 KB
testcase_33 AC 147 ms
13,040 KB
testcase_34 AC 39 ms
11,776 KB
testcase_35 AC 40 ms
11,648 KB
testcase_36 AC 99 ms
12,160 KB
testcase_37 AC 246 ms
13,696 KB
testcase_38 AC 337 ms
13,940 KB
testcase_39 AC 82 ms
11,904 KB
testcase_40 AC 126 ms
13,056 KB
testcase_41 AC 47 ms
11,648 KB
testcase_42 AC 45 ms
11,520 KB
testcase_43 AC 91 ms
12,160 KB
testcase_44 AC 188 ms
13,696 KB
testcase_45 AC 50 ms
11,648 KB
testcase_46 AC 54 ms
11,648 KB
testcase_47 AC 162 ms
12,288 KB
testcase_48 AC 299 ms
15,432 KB
testcase_49 AC 78 ms
12,032 KB
testcase_50 AC 55 ms
11,776 KB
testcase_51 AC 196 ms
13,568 KB
testcase_52 AC 86 ms
12,032 KB
testcase_53 AC 41 ms
11,520 KB
testcase_54 AC 61 ms
12,032 KB
testcase_55 AC 41 ms
11,264 KB
testcase_56 AC 39 ms
11,264 KB
testcase_57 AC 52 ms
11,776 KB
testcase_58 AC 141 ms
12,544 KB
testcase_59 AC 65 ms
11,776 KB
testcase_60 AC 355 ms
15,288 KB
testcase_61 AC 325 ms
15,296 KB
testcase_62 AC 31 ms
11,008 KB
testcase_63 AC 31 ms
11,136 KB
testcase_64 AC 33 ms
11,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import array
import collections
import itertools
import sys


DELTAS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
INF = 10 ** 8
REC_LIMIT = 10000
FlowEdge = collections.namedtuple("FlowEdge", "source sink capacity")


class FlowNetwork(object):

    def __init__(self, num_vertices):
        self.adj_edges = [set() for _ in range(num_vertices)]
        self.flow = None
        self.rev_edge = dict()
        self.used = None

    def get_edges_from(self, vertex):
        return self.adj_edges[vertex]

    def add_edge(self, source, sink, capacity=1):
        assert source != sink
        forward_edge = FlowEdge(source, sink, capacity)
        backward_edge = FlowEdge(sink, source, 0)
        self.rev_edge[forward_edge] = backward_edge
        self.rev_edge[backward_edge] = forward_edge
        self.adj_edges[source].add(forward_edge)
        self.adj_edges[sink].add(backward_edge)

    def dfs(self, source, sink, flow):
        if source == sink:
            return flow
        self.used[source] = True
        for edge in self.get_edges_from(source):
            rest = edge.capacity - self.flow[edge]
            if self.used[edge.sink] or rest <= 0:
                continue
            d = self.dfs(edge.sink, sink, min(flow, rest))
            if d > 0:
                self.flow[edge] += d
                self.flow[self.rev_edge[edge]] -= d
                return d
        return 0

    def ford_fulkerson(self, source, sink):
        self.flow = collections.defaultdict(int)
        max_flow = 0
        while True:
            self.used = collections.defaultdict(bool)
            df = self.dfs(source, sink, INF)
            if df == 0:
                return max_flow
            else:
                max_flow += df


def main():
    sys.setrecursionlimit(REC_LIMIT)
    n, m = map(int, input().split())
    board = [input() for _ in range(n)]
    num_vs = n * m + 2
    source = n * m
    sink = n * m + 1
    fn = FlowNetwork(num_vs)
    white = 0
    black = 0
    for r0, c0 in itertools.product(range(n), range(m)):
        if board[r0][c0] == "w":
            white += 1
            w = r0 * m + c0
            fn.add_edge(source, w)
            for dr, dc in DELTAS:
                r, c = r0 + dr, c0 + dc
                if r < 0 or r >= n or c < 0 or c >= m:
                    continue
                elif board[r][c] == "b":
                    b = r * m + c
                    fn.add_edge(w, b)
                    fn.add_edge(b, sink)
        elif board[r0][c0] == "b":
            black += 1
    mf = fn.ford_fulkerson(source, sink)
    ans = mf * 100
    white -= mf
    black -= mf
    meth2 = min(white, black)
    ans += meth2 * 10
    white -= meth2
    black -= meth2
    ans += max(white, black)
    print(ans)


if __name__ == '__main__':
    main()
0