結果

問題 No.957 植林
ユーザー mkawa2mkawa2
提出日時 2023-11-30 15:57:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,139 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 82,848 KB
実行使用メモリ 111,644 KB
最終ジャッジ日時 2024-09-26 14:05:59
合計ジャッジ時間 7,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
59,904 KB
testcase_01 AC 40 ms
54,656 KB
testcase_02 AC 42 ms
54,528 KB
testcase_03 AC 221 ms
93,696 KB
testcase_04 AC 208 ms
94,080 KB
testcase_05 AC 272 ms
99,448 KB
testcase_06 AC 221 ms
100,224 KB
testcase_07 AC 229 ms
97,560 KB
testcase_08 AC 144 ms
92,160 KB
testcase_09 AC 140 ms
92,068 KB
testcase_10 AC 152 ms
92,856 KB
testcase_11 AC 166 ms
92,212 KB
testcase_12 AC 166 ms
92,360 KB
testcase_13 AC 135 ms
93,476 KB
testcase_14 AC 149 ms
91,208 KB
testcase_15 AC 134 ms
92,672 KB
testcase_16 AC 125 ms
93,056 KB
testcase_17 AC 127 ms
93,280 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

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

from collections import deque
class PushRelabel:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.initial = [N]*N
        self.zeros = [0]*(N+1)

    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)

    # Global labeling
    def bfs(self, H, D, t, que=deque()):
        que.append(t)
        G = self.G
        H[:] = self.initial
        H[t] = 0
        D[:] = self.zeros
        D[0] = 1
        N = self.N
        c = N-1
        while que:
            v = que.popleft()
            d = H[v] + 1
            for w, cap, backward in G[v]:
                if H[w] <= d or backward[1] == 0:
                    continue
                H[w] = d
                D[d] += 1
                c -= 1
                que.append(w)
        D[N] = c
        return d # gap

    # FIFO preflow-push algorithm
    def flow(self, s, t):
        N = self.N
        H = [0]*N # height
        F = [0]*N # excess flow
        D = [0]*(N+1) # distance label
        active = [0]*N # active node

        G = self.G

        que = deque([s])
        F[s] = 10**18
        active[s] = 1

        gap = self.bfs(H, D, t)

        cnt = 0
        while que:
            v = que.popleft()
            if v == t:
                continue

            hv = H[v]
            # Gap-relabeling
            if hv > gap:
                D[hv] -= 1
                D[N] += 1
                hv = H[v] = N
                continue
            # push
            rest = F[v]
            for e in G[v]:
                w, cap, backward = e
                if cap and hv > H[w] < gap:
                    d = min(rest, cap)
                    e[1] -= d
                    backward[1] += d
                    rest -= d
                    F[w] += d
                    if not active[w]:
                        que.append(w)
                        active[w] = 1
                    if rest == 0:
                        break
            F[v] = rest

            if rest == 0:
                active[v] = 0
                continue

            # relabel
            h0 = H[v]
            hv = N
            for w, cap, backward in G[v]:
                if cap and hv > H[w] + 1 <= gap:
                    hv = H[w] + 1
            if hv != h0:
                D[h0] -= 1
                if D[h0] == 0 and h0 < gap:
                    gap = h0
                    hv = N
                elif hv == gap:
                    gap += 1
                D[hv] += 1

            H[v] = hv
            if hv < N:
                que.append(v)
            else:
                active[v] = 0

            cnt += 1
            if cnt % N == 0:
                gap = self.bfs(H, D, t)
        return F[t]

h,w=LI()
gg=LLI(h)
rr=LI()
cc=LI()

s=h+w
t=s+1
mf=PushRelabel(h+w+2)

ans=0
for u,r in enumerate(rr):
    mf.add_edge(s,u,r)
    mf.add_edge(u,t,sum(gg[u]))
    ans+=r
for u,c in enumerate(cc,h):
    mf.add_edge(s,u,c)
    ans+=c
    for v in range(h):
        mf.add_edge(u,v,gg[v][u-h])

ans-=mf.flow(s,t)
print(ans)
0