結果

問題 No.957 植林
ユーザー tcltktcltk
提出日時 2021-06-28 05:05:01
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,646 bytes
コンパイル時間 441 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 184,196 KB
最終ジャッジ日時 2023-09-07 18:12:39
合計ジャッジ時間 25,126 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 207 ms
80,816 KB
testcase_01 AC 208 ms
80,964 KB
testcase_02 AC 210 ms
80,988 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 1,770 ms
179,272 KB
testcase_06 TLE -
testcase_07 AC 1,930 ms
177,788 KB
testcase_08 AC 732 ms
157,248 KB
testcase_09 AC 666 ms
156,644 KB
testcase_10 AC 848 ms
167,460 KB
testcase_11 AC 761 ms
161,524 KB
testcase_12 AC 777 ms
159,912 KB
testcase_13 AC 597 ms
150,520 KB
testcase_14 AC 672 ms
166,760 KB
testcase_15 AC 665 ms
156,840 KB
testcase_16 AC 611 ms
150,604 KB
testcase_17 AC 632 ms
152,956 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 #

#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq


def input():
    return sys.stdin.readline()[:-1]


# sys.setrecursionlimit(1000000)

# _INPUT = """2 3
# 1 2 3
# 4 5 6
# 6 2
# 1 7 3
# """
# sys.stdin = io.StringIO(_INPUT)


class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(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):
        depth = [-1] * self.n
        depth[s] = 0
        q = collections.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, INF)
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, INF)

INF = 10**15
H, W = map(int, input().split())
G = [list(map(int, input().split())) for _ in range(H)]
R = list(map(int, input().split()))
C = list(map(int, input().split()))

mf = Dinic(H*W+H+W+2)
S = 0
T = H*W+H+W+1

for h in range(H):
    for w in range(W):
        i = h*W+w+1
        mf.add_link(S, i, G[h][w])
        mf.add_link(i, T, 0)

# 行ボーナス
for h in range(H):
    k = H*W+1+h
    mf.add_link(k, T, R[h])
    for w in range(W):
        i = h*W+w+1
        mf.add_link(i, k, INF)

# 列ボーナス
for w in range(W):
    k = H*W+H+1+w
    mf.add_link(k, T, C[w])
    for h in range(H):
        i = h*W+w+1
        mf.add_link(i, k, INF)

flow = mf.max_flow(S, T)
score = sum(R) + sum(C) - flow
print(score)
0