結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー optopt
提出日時 2020-12-08 20:40:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 2,462 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 81,648 KB
最終ジャッジ日時 2024-09-19 22:01:00
合計ジャッジ時間 22,547 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,528 KB
testcase_01 AC 45 ms
54,144 KB
testcase_02 AC 47 ms
54,784 KB
testcase_03 AC 80 ms
70,528 KB
testcase_04 AC 46 ms
54,272 KB
testcase_05 AC 45 ms
54,144 KB
testcase_06 AC 42 ms
54,656 KB
testcase_07 AC 382 ms
80,348 KB
testcase_08 AC 408 ms
80,744 KB
testcase_09 AC 403 ms
80,308 KB
testcase_10 AC 401 ms
80,312 KB
testcase_11 AC 216 ms
79,508 KB
testcase_12 AC 167 ms
78,516 KB
testcase_13 AC 182 ms
78,768 KB
testcase_14 AC 191 ms
78,668 KB
testcase_15 AC 377 ms
80,344 KB
testcase_16 AC 379 ms
80,084 KB
testcase_17 AC 380 ms
79,880 KB
testcase_18 AC 416 ms
80,812 KB
testcase_19 AC 115 ms
76,968 KB
testcase_20 AC 124 ms
77,444 KB
testcase_21 AC 118 ms
77,320 KB
testcase_22 AC 121 ms
77,884 KB
testcase_23 AC 137 ms
77,696 KB
testcase_24 AC 136 ms
77,700 KB
testcase_25 AC 137 ms
77,784 KB
testcase_26 AC 138 ms
77,748 KB
testcase_27 AC 92 ms
76,672 KB
testcase_28 AC 97 ms
76,772 KB
testcase_29 AC 94 ms
76,672 KB
testcase_30 AC 93 ms
76,876 KB
testcase_31 AC 114 ms
77,472 KB
testcase_32 AC 134 ms
77,828 KB
testcase_33 AC 137 ms
78,528 KB
testcase_34 AC 163 ms
79,172 KB
testcase_35 AC 119 ms
77,416 KB
testcase_36 AC 105 ms
77,048 KB
testcase_37 AC 101 ms
76,856 KB
testcase_38 AC 101 ms
77,020 KB
testcase_39 AC 139 ms
77,784 KB
testcase_40 AC 137 ms
77,972 KB
testcase_41 AC 130 ms
77,872 KB
testcase_42 AC 136 ms
78,472 KB
testcase_43 AC 245 ms
78,744 KB
testcase_44 AC 250 ms
78,944 KB
testcase_45 AC 328 ms
79,220 KB
testcase_46 AC 308 ms
79,220 KB
testcase_47 AC 206 ms
79,008 KB
testcase_48 AC 201 ms
78,644 KB
testcase_49 AC 199 ms
78,780 KB
testcase_50 AC 210 ms
79,276 KB
testcase_51 AC 273 ms
79,500 KB
testcase_52 AC 253 ms
79,060 KB
testcase_53 AC 430 ms
81,176 KB
testcase_54 AC 411 ms
80,680 KB
testcase_55 AC 376 ms
80,344 KB
testcase_56 AC 374 ms
80,264 KB
testcase_57 AC 326 ms
79,012 KB
testcase_58 AC 349 ms
79,792 KB
testcase_59 AC 341 ms
79,348 KB
testcase_60 AC 376 ms
80,076 KB
testcase_61 AC 370 ms
80,252 KB
testcase_62 AC 393 ms
79,752 KB
testcase_63 AC 400 ms
79,916 KB
testcase_64 AC 398 ms
80,660 KB
testcase_65 AC 432 ms
81,648 KB
testcase_66 AC 420 ms
80,488 KB
testcase_67 AC 417 ms
81,004 KB
testcase_68 AC 432 ms
80,440 KB
testcase_69 AC 378 ms
80,108 KB
testcase_70 AC 453 ms
81,040 KB
testcase_71 AC 474 ms
81,320 KB
testcase_72 AC 449 ms
80,832 KB
testcase_73 AC 397 ms
80,372 KB
testcase_74 AC 473 ms
81,576 KB
testcase_75 AC 174 ms
78,808 KB
testcase_76 AC 171 ms
78,512 KB
testcase_77 AC 177 ms
78,848 KB
testcase_78 AC 385 ms
80,788 KB
testcase_79 AC 386 ms
79,936 KB
testcase_80 AC 458 ms
80,956 KB
testcase_81 AC 257 ms
79,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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)
    cs = [0] * (n+1)
    for j, (a2, b2, _, _) in enumerate(edges):
        if y[j] > 0:
            g.add_multi_edge(a2, b2, y[j], y[j])
        cs[a2] += y[j]
        cs[b2] += y[j]
    for v in range(1, n+1):
        if v == a or v == b:
            g.add_edge(s, v, 2*k*n)
        elif cs[v] > 0:
            g.add_edge(s, v, cs[v])
        g.add_edge(v, t, 2*k)
    f = g.flow(s, t)
    z = f // 2 - k - ysum
    y[i] = min(z, d)
    ysum += y[i]
    ans += c * y[i]

if ysum < k*(n-1):
    ans = -1
print(ans)
0