結果
問題 | No.1316 Maximum Minimum Spanning Tree |
ユーザー | opt |
提出日時 | 2020-12-12 22:36:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 391 ms / 2,000 ms |
コード長 | 3,129 bytes |
コンパイル時間 | 398 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 80,900 KB |
最終ジャッジ日時 | 2024-09-19 22:31:56 |
合計ジャッジ時間 | 18,963 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
54,400 KB |
testcase_01 | AC | 40 ms
54,272 KB |
testcase_02 | AC | 38 ms
54,144 KB |
testcase_03 | AC | 61 ms
69,120 KB |
testcase_04 | AC | 39 ms
54,528 KB |
testcase_05 | AC | 37 ms
54,144 KB |
testcase_06 | AC | 39 ms
54,272 KB |
testcase_07 | AC | 321 ms
80,408 KB |
testcase_08 | AC | 330 ms
80,900 KB |
testcase_09 | AC | 296 ms
80,036 KB |
testcase_10 | AC | 301 ms
79,248 KB |
testcase_11 | AC | 175 ms
78,644 KB |
testcase_12 | AC | 145 ms
78,124 KB |
testcase_13 | AC | 145 ms
78,016 KB |
testcase_14 | AC | 164 ms
78,632 KB |
testcase_15 | AC | 335 ms
80,448 KB |
testcase_16 | AC | 340 ms
79,736 KB |
testcase_17 | AC | 323 ms
79,768 KB |
testcase_18 | AC | 345 ms
80,404 KB |
testcase_19 | AC | 109 ms
76,984 KB |
testcase_20 | AC | 115 ms
77,504 KB |
testcase_21 | AC | 104 ms
77,256 KB |
testcase_22 | AC | 111 ms
77,504 KB |
testcase_23 | AC | 123 ms
77,620 KB |
testcase_24 | AC | 126 ms
78,180 KB |
testcase_25 | AC | 122 ms
77,912 KB |
testcase_26 | AC | 123 ms
78,120 KB |
testcase_27 | AC | 90 ms
76,980 KB |
testcase_28 | AC | 93 ms
77,000 KB |
testcase_29 | AC | 97 ms
76,704 KB |
testcase_30 | AC | 102 ms
76,896 KB |
testcase_31 | AC | 108 ms
77,212 KB |
testcase_32 | AC | 124 ms
77,872 KB |
testcase_33 | AC | 113 ms
77,620 KB |
testcase_34 | AC | 116 ms
77,456 KB |
testcase_35 | AC | 99 ms
76,604 KB |
testcase_36 | AC | 90 ms
76,820 KB |
testcase_37 | AC | 96 ms
76,824 KB |
testcase_38 | AC | 92 ms
76,696 KB |
testcase_39 | AC | 114 ms
77,444 KB |
testcase_40 | AC | 106 ms
77,312 KB |
testcase_41 | AC | 111 ms
77,568 KB |
testcase_42 | AC | 113 ms
77,556 KB |
testcase_43 | AC | 237 ms
79,616 KB |
testcase_44 | AC | 239 ms
79,100 KB |
testcase_45 | AC | 258 ms
78,772 KB |
testcase_46 | AC | 293 ms
79,044 KB |
testcase_47 | AC | 196 ms
78,648 KB |
testcase_48 | AC | 191 ms
78,848 KB |
testcase_49 | AC | 187 ms
78,884 KB |
testcase_50 | AC | 196 ms
79,324 KB |
testcase_51 | AC | 280 ms
80,156 KB |
testcase_52 | AC | 245 ms
78,992 KB |
testcase_53 | AC | 323 ms
80,060 KB |
testcase_54 | AC | 343 ms
79,744 KB |
testcase_55 | AC | 279 ms
79,076 KB |
testcase_56 | AC | 246 ms
78,656 KB |
testcase_57 | AC | 246 ms
78,668 KB |
testcase_58 | AC | 267 ms
79,028 KB |
testcase_59 | AC | 272 ms
78,632 KB |
testcase_60 | AC | 318 ms
79,496 KB |
testcase_61 | AC | 286 ms
79,068 KB |
testcase_62 | AC | 333 ms
79,852 KB |
testcase_63 | AC | 321 ms
80,144 KB |
testcase_64 | AC | 287 ms
79,560 KB |
testcase_65 | AC | 299 ms
79,448 KB |
testcase_66 | AC | 322 ms
79,548 KB |
testcase_67 | AC | 332 ms
79,632 KB |
testcase_68 | AC | 295 ms
79,812 KB |
testcase_69 | AC | 316 ms
79,544 KB |
testcase_70 | AC | 358 ms
80,220 KB |
testcase_71 | AC | 386 ms
80,256 KB |
testcase_72 | AC | 371 ms
80,676 KB |
testcase_73 | AC | 329 ms
79,740 KB |
testcase_74 | AC | 391 ms
80,204 KB |
testcase_75 | AC | 132 ms
78,000 KB |
testcase_76 | AC | 150 ms
78,060 KB |
testcase_77 | AC | 145 ms
77,904 KB |
testcase_78 | AC | 325 ms
80,040 KB |
testcase_79 | AC | 316 ms
79,648 KB |
testcase_80 | AC | 345 ms
79,900 KB |
testcase_81 | AC | 220 ms
78,660 KB |
ソースコード
# Dinic's algorithm from: # https://tjkendev.github.io/procon-library/python/max_flow/dinic.html from collections import deque class Dinic: def __init__(self, N): self.N = N self.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.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow ########################################## n, m, k = map(int, input().split()) edges = [list(map(int, input().split())) for _ in range(m)] edges.sort(key=lambda x: x[2]) y = [0] * m s, t = 0, n+1 ysum = ans = 0 # 多重辺を気にせず張る # for i, (a, b, c, d) in enumerate(edges): # g = Dinic(n+2) # for j, (a2, b2, _, _) in enumerate(edges): # if y[j] > 0: # 定数倍改善のため,容量 0 の辺を追加しない.追加しても問題無い # g.add_edge(s, a2, y[j]) # g.add_edge(a2, b2, y[j]) # for v in range(1, n+1): # g.add_edge(v, t, k) # g.add_edge(s, a, k*n) # g.add_edge(s, b, k*n) # f = g.flow(s, t) # z = f - k - ysum # y[i] = min(z, d) # ysum += y[i] # ans += c * y[i] # 多重辺を張らないようにして定数倍高速化 cs = [0] * (n+1) for i, (a, b, c, d) in enumerate(edges): g = Dinic(n+2) for j, (a2, b2, _, _) in enumerate(edges): if y[j] > 0: # 定数倍改善のため,容量 0 の辺を追加しない.追加しても問題無い g.add_edge(a2, b2, y[j]) for v in range(1, n+1): g.add_edge(v, t, k) if v == a or v == b: g.add_edge(s, v, k*n) elif cs[v] > 0: g.add_edge(s, v, cs[v]) f = g.flow(s, t) z = f - k - ysum y[i] = min(z, d) ysum += y[i] ans += c * y[i] cs[a] += y[i] if ysum < k*(n-1): ans = -1 print(ans)