結果

問題 No.957 植林
ユーザー zundamo-tizundamo-ti
提出日時 2021-03-02 06:15:31
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,398 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 82,632 KB
実行使用メモリ 187,432 KB
最終ジャッジ日時 2024-04-14 04:26:05
合計ジャッジ時間 16,667 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
62,644 KB
testcase_01 AC 40 ms
54,996 KB
testcase_02 AC 39 ms
56,004 KB
testcase_03 AC 1,535 ms
162,940 KB
testcase_04 AC 1,676 ms
150,772 KB
testcase_05 AC 1,281 ms
154,868 KB
testcase_06 AC 1,869 ms
162,068 KB
testcase_07 AC 1,460 ms
153,716 KB
testcase_08 AC 455 ms
138,468 KB
testcase_09 AC 403 ms
137,496 KB
testcase_10 AC 542 ms
144,832 KB
testcase_11 AC 485 ms
140,960 KB
testcase_12 AC 492 ms
141,672 KB
testcase_13 AC 346 ms
130,016 KB
testcase_14 AC 407 ms
141,980 KB
testcase_15 AC 404 ms
138,948 KB
testcase_16 AC 355 ms
131,300 KB
testcase_17 AC 357 ms
131,544 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
INF = float('inf')

class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_edge(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):
        from collections import deque

        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'))

def main():
    H, W = map(int, readline().split())
    G = tuple(tuple(map(int, readline().split())) for _ in range(H))
    R = tuple(map(int, readline().split()))
    C = tuple(map(int, readline().split()))
    
    V = H * W + H + W + 2
    dinic = Dinic(V)
    s, t = V - 2, V - 1
    add = dinic.add_edge
    for h in range(H):
        for w in range(W):
            v = W * h + w
            add(s, v, G[h][w])
            add(v, h + H * W, INF)
            add(v, w + H * W + H, INF)
    for h in range(H):
        add(h + H * W, t, R[h])
    for w in range(W):
        add(w + H * W + H, t, C[w])
    flow = dinic.max_flow(s, t)
    print(sum(R) + sum(C) - flow)

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