結果

問題 No.957 植林
ユーザー mymelochanmymelochan
提出日時 2021-09-14 23:00:39
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,081 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 101,316 KB
最終ジャッジ日時 2023-09-09 22:12:37
合計ジャッジ時間 17,988 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,876 KB
testcase_01 AC 94 ms
71,364 KB
testcase_02 AC 95 ms
71,396 KB
testcase_03 AC 556 ms
98,828 KB
testcase_04 AC 654 ms
97,468 KB
testcase_05 AC 573 ms
99,856 KB
testcase_06 AC 811 ms
100,548 KB
testcase_07 AC 535 ms
98,072 KB
testcase_08 AC 439 ms
97,120 KB
testcase_09 AC 295 ms
97,472 KB
testcase_10 AC 346 ms
98,160 KB
testcase_11 AC 494 ms
97,196 KB
testcase_12 AC 319 ms
97,632 KB
testcase_13 AC 272 ms
95,056 KB
testcase_14 AC 400 ms
96,436 KB
testcase_15 AC 336 ms
96,984 KB
testcase_16 AC 252 ms
95,472 KB
testcase_17 AC 327 ms
96,120 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
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 #

#https://tjkendev.github.io/procon-library/python/max_flow/dinic.html
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

    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, active, t, que=deque()):
        N = self.N
        B = [[] for i in range(N)]
        que.append(t)
        G = self.G
        H[:] = self.initial
        H[t] = 0
        D[:] = self.zeros
        D[0] = 1
        cur = 0
        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
                if active[w] and d < N:
                    B[d].append(w)
                    cur = d
                if d < N:
                    D[d] += 1
                que.append(w)
        return B, cur, d

    # Highest 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

        F[s] = 10**18
        active[s] = 1

        B, cur, gap = self.bfs(H, D, active, t)
        B[cur].append(s)

        cnt = 0
        while 1:
            while cur >= 0 and not B[cur]:
                cur -= 1
            if cur < 0:
                break
            v = B[cur].pop()
            if v == t:
                continue

            hv = H[v]
            # Gap-relabeling
            if hv > gap:
                if hv < N:
                    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]:
                        hw = H[w]
                        B[hw].append(w)
                        if cur < hw:
                            cur = hw
                        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 h0 != hv:
                D[h0] -= 1
                if D[h0] == 0 and h0 < gap:
                    gap = h0
                    hv = N
                elif hv == gap:
                    gap += 1
                if hv < N:
                    D[hv] += 1

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

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

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

S = sum(R)+sum(C)
mf = PushRelabel(H+W+2)
for h in range(H):
    mf.add_edge(H+W,h,sum(G[h]))
    for w in range(W):
        mf.add_edge(h,H+w,G[h][w])
    mf.add_edge(h,H+W+1,R[h])

for w in range(W):
    mf.add_edge(H+W,H+w,0)
    mf.add_edge(H+w,H+W+1,C[w])

print(S-mf.flow(H+W,H+W+1))
0