結果
問題 | No.2328 Build Walls |
ユーザー |
![]() |
提出日時 | 2023-05-15 17:13:43 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,464 bytes |
コンパイル時間 | 143 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 773,248 KB |
最終ジャッジ日時 | 2024-11-30 22:23:32 |
合計ジャッジ時間 | 95,057 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 10 TLE * 9 MLE * 15 |
ソースコード
from collections import dequeclass Dinic:def __init__(self, N):self.N = Nself.G = [[] for i in range(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)def bfs(self, s, t):self.level = level = [None]*self.Ndeq = deque([s])level[s] = 0G = self.Gwhile deq:v = deq.popleft()lv = level[v] + 1for w, cap, _ in G[v]:if cap and level[w] is None:level[w] = lvdeq.append(w)return level[t] is not Nonedef dfs(self, v, t, f):if v == t:return flevel = self.levelfor e in self.it[v]:w, cap, rev = eif cap and level[v] < level[w]:d = self.dfs(w, t, min(f, cap))if d:e[1] -= drev[1] += dreturn dreturn 0def flow(self, s, t):flow = 0INF = 10**9 + 7G = self.Gwhile self.bfs(s, t):*self.it, = map(iter, self.G)f = INFwhile f:f = self.dfs(s, t, INF)flow += freturn flowh, w = map(int, input().split())assert (3 <= min(h, w) <= max(h, w) <= 800)h -= 2a = [list(map(int, input().split())) for _ in range(h)]def ij(i, j, t): return (i*w+j)*2+tdinic = Dinic(h*w*2+2)inf = 10**10s = h*w*2for i in range(h):for j in range(w):if a[i][j] != -1:dinic.add_edge(ij(i, j, 0), ij(i, j, 1), a[i][j])else:dinic.add_edge(ij(i, j, 0), ij(i, j, 1), inf)if i != h-1:dinic.add_edge(ij(i, j, 1), ij(i+1, j, 0), inf)dinic.add_edge(ij(i+1, j, 1), ij(i, j, 0), inf)if j != w-1:dinic.add_edge(ij(i, j, 1), ij(i, j+1, 0), inf)dinic.add_edge(ij(i, j+1, 1), ij(i, j, 0), inf)for j in range(w):dinic.add_edge(s, ij(0, j, 0), inf)dinic.add_edge(ij(h-1, j, 1), s+1, inf)ans = dinic.flow(s, s+1)if ans >= inf:print(-1)else:print(ans)