結果
問題 | No.957 植林 |
ユーザー | maspy |
提出日時 | 2020-04-10 00:05:31 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,689 bytes |
コンパイル時間 | 391 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 99,220 KB |
最終ジャッジ日時 | 2024-09-14 02:37:29 |
合計ジャッジ時間 | 7,356 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
59,520 KB |
testcase_01 | AC | 50 ms
54,016 KB |
testcase_02 | AC | 47 ms
54,016 KB |
testcase_03 | AC | 237 ms
92,056 KB |
testcase_04 | AC | 249 ms
90,852 KB |
testcase_05 | AC | 237 ms
93,300 KB |
testcase_06 | AC | 284 ms
94,492 KB |
testcase_07 | AC | 244 ms
91,776 KB |
testcase_08 | AC | 171 ms
92,620 KB |
testcase_09 | AC | 166 ms
92,640 KB |
testcase_10 | AC | 182 ms
93,552 KB |
testcase_11 | AC | 179 ms
92,892 KB |
testcase_12 | AC | 175 ms
92,412 KB |
testcase_13 | AC | 150 ms
90,532 KB |
testcase_14 | AC | 168 ms
95,052 KB |
testcase_15 | AC | 159 ms
92,360 KB |
testcase_16 | AC | 152 ms
90,724 KB |
testcase_17 | AC | 155 ms
90,952 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 | -- | - |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import deque class Dinic: def __init__(self, N, source, sink): self.N = N self.G = [[] for _ in range(N)] self.source = source self.sink = sink def add_edge(self, fr, to, cap): n1 = len(self.G[fr]) n2 = len(self.G[to]) self.G[fr].append([to, cap, n2]) self.G[to].append([fr, 0, n1]) # 逆辺を cap 0 で追加 def add_edge_undirected(self, fr, to, cap): n1 = len(self.G[fr]) n2 = len(self.G[to]) self.G[fr].append([to, cap, n2]) self.G[to].append([fr, cap, n1]) def bfs(self): level = [0] * self.N G = self.G source = self.source sink = self.sink q = deque([source]) level[source] = 1 pop = q.popleft append = q.append while q: v = pop() lv = level[v] + 1 for to, cap, rev in G[v]: if not cap: continue if level[to]: continue level[to] = lv if to == sink: self.level = level return append(to) self.level = level def dfs(self): INF = 10**18 G = self.G sink = self.sink prog = self.progress level = self.level ascend = False ff = 0 stack = [(self.source, INF)] while stack: if ff: # このまま更新だけして戻っていく v, f = stack.pop() p = prog[v] to, cap, rev = G[v][p] G[v][p][1] -= ff G[to][rev][1] += ff continue v, f = stack[-1] if v == sink: ff = f stack.pop() continue E = G[v] lv = level[v] if ascend: # 流せずに戻ってきた prog[v] += 1 find_flag = False for i in range(prog[v], len(E)): to, cap, rev = E[i] prog[v] = i if not cap: continue if level[to] <= lv: continue find_flag = True break if not find_flag: ascend = True stack.pop() continue ascend = False x = f if f < cap else cap stack.append((to, x)) return ff def max_flow(self): flow = 0 while True: self.bfs() if not self.level[self.sink]: return flow self.progress = [0] * self.N while True: f = self.dfs() if not f: break flow += f return flow H, W = map(int, readline().split()) G = [] for _ in range(H): G += list(map(int, readline().split())) R = list(map(int, readline().split())) C = list(map(int, readline().split())) source = H + W sink = H + W + 1 dinic = Dinic(H + W + 2, source, sink) add = dinic.add_edge base_score = sum(C) for i in range(H): row_cost = sum(G[W * i: W * i + W]) if R[i] > row_cost: base_score += R[i] - row_cost R[i] = row_cost add(source, i, row_cost - R[i]) for j in range(W): add(H + j, sink, C[j]) for n, x in enumerate(G): i, j = divmod(n, W) add(i, H + j, x) f = dinic.max_flow() answer = base_score - f print(answer)