結果
問題 | No.957 植林 |
ユーザー | maspy |
提出日時 | 2020-04-10 00:05:08 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,689 bytes |
コンパイル時間 | 150 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 37,248 KB |
最終ジャッジ日時 | 2024-09-14 02:37:05 |
合計ジャッジ時間 | 10,990 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
16,384 KB |
testcase_01 | AC | 33 ms
10,880 KB |
testcase_02 | AC | 32 ms
10,880 KB |
testcase_03 | AC | 585 ms
33,024 KB |
testcase_04 | AC | 639 ms
31,232 KB |
testcase_05 | AC | 570 ms
34,048 KB |
testcase_06 | AC | 858 ms
35,968 KB |
testcase_07 | AC | 619 ms
32,384 KB |
testcase_08 | AC | 272 ms
32,896 KB |
testcase_09 | AC | 262 ms
32,640 KB |
testcase_10 | AC | 300 ms
34,432 KB |
testcase_11 | AC | 289 ms
32,896 KB |
testcase_12 | AC | 275 ms
32,640 KB |
testcase_13 | AC | 220 ms
29,824 KB |
testcase_14 | AC | 275 ms
35,712 KB |
testcase_15 | AC | 241 ms
32,384 KB |
testcase_16 | AC | 224 ms
30,592 KB |
testcase_17 | AC | 235 ms
30,720 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)