結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー 👑 hitonanodehitonanode
提出日時 2020-12-12 22:09:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 397 ms / 2,000 ms
コード長 2,267 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 81,696 KB
実行使用メモリ 79,764 KB
最終ジャッジ日時 2023-10-20 02:42:44
合計ジャッジ時間 20,546 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,588 KB
testcase_01 AC 42 ms
55,588 KB
testcase_02 AC 42 ms
55,588 KB
testcase_03 AC 85 ms
75,864 KB
testcase_04 AC 42 ms
55,588 KB
testcase_05 AC 41 ms
55,588 KB
testcase_06 AC 41 ms
55,588 KB
testcase_07 AC 315 ms
78,492 KB
testcase_08 AC 324 ms
78,928 KB
testcase_09 AC 317 ms
78,308 KB
testcase_10 AC 339 ms
78,764 KB
testcase_11 AC 166 ms
78,000 KB
testcase_12 AC 153 ms
77,304 KB
testcase_13 AC 144 ms
77,300 KB
testcase_14 AC 145 ms
77,096 KB
testcase_15 AC 289 ms
78,604 KB
testcase_16 AC 322 ms
78,768 KB
testcase_17 AC 285 ms
78,368 KB
testcase_18 AC 315 ms
78,716 KB
testcase_19 AC 113 ms
76,400 KB
testcase_20 AC 116 ms
77,164 KB
testcase_21 AC 118 ms
77,080 KB
testcase_22 AC 116 ms
77,152 KB
testcase_23 AC 121 ms
77,208 KB
testcase_24 AC 131 ms
77,748 KB
testcase_25 AC 117 ms
77,128 KB
testcase_26 AC 123 ms
77,044 KB
testcase_27 AC 99 ms
76,504 KB
testcase_28 AC 102 ms
76,360 KB
testcase_29 AC 103 ms
76,564 KB
testcase_30 AC 108 ms
76,492 KB
testcase_31 AC 121 ms
77,028 KB
testcase_32 AC 126 ms
77,252 KB
testcase_33 AC 114 ms
77,160 KB
testcase_34 AC 121 ms
77,112 KB
testcase_35 AC 105 ms
76,444 KB
testcase_36 AC 105 ms
76,424 KB
testcase_37 AC 106 ms
76,464 KB
testcase_38 AC 110 ms
76,676 KB
testcase_39 AC 122 ms
77,116 KB
testcase_40 AC 127 ms
77,196 KB
testcase_41 AC 113 ms
76,544 KB
testcase_42 AC 111 ms
76,532 KB
testcase_43 AC 294 ms
78,200 KB
testcase_44 AC 296 ms
78,200 KB
testcase_45 AC 332 ms
78,372 KB
testcase_46 AC 359 ms
78,876 KB
testcase_47 AC 192 ms
77,652 KB
testcase_48 AC 184 ms
77,568 KB
testcase_49 AC 175 ms
77,544 KB
testcase_50 AC 165 ms
77,492 KB
testcase_51 AC 304 ms
78,408 KB
testcase_52 AC 303 ms
78,796 KB
testcase_53 AC 391 ms
79,220 KB
testcase_54 AC 363 ms
79,200 KB
testcase_55 AC 328 ms
78,216 KB
testcase_56 AC 335 ms
78,548 KB
testcase_57 AC 334 ms
78,368 KB
testcase_58 AC 361 ms
79,232 KB
testcase_59 AC 327 ms
79,500 KB
testcase_60 AC 304 ms
79,524 KB
testcase_61 AC 387 ms
79,064 KB
testcase_62 AC 358 ms
78,696 KB
testcase_63 AC 363 ms
78,932 KB
testcase_64 AC 294 ms
78,556 KB
testcase_65 AC 345 ms
79,252 KB
testcase_66 AC 357 ms
79,016 KB
testcase_67 AC 358 ms
78,884 KB
testcase_68 AC 314 ms
78,608 KB
testcase_69 AC 352 ms
79,344 KB
testcase_70 AC 327 ms
78,948 KB
testcase_71 AC 338 ms
78,672 KB
testcase_72 AC 397 ms
79,764 KB
testcase_73 AC 292 ms
78,360 KB
testcase_74 AC 343 ms
79,052 KB
testcase_75 AC 133 ms
77,412 KB
testcase_76 AC 140 ms
77,604 KB
testcase_77 AC 138 ms
77,596 KB
testcase_78 AC 294 ms
78,524 KB
testcase_79 AC 285 ms
79,404 KB
testcase_80 AC 307 ms
78,696 KB
testcase_81 AC 195 ms
77,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Python, assuming AC
from collections import deque

class MaxFlow:
    def __init__(self, N: int) -> None:
        self.N = N
        self.edges = [[] for _ in range(N)]

    def add_edge(self, s: int, t: int, cap: int) -> None:
        e = [t, cap, None]
        e[2] = erev = [s, 0, e]
        self.edges[s].append(e)
        self.edges[t].append(erev)

    def bfs(self, s: int, t: int) -> bool:
        self.level = [-1] * self.N
        self.level[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            lnxt = self.level[v] + 1
            for to, cap, _ in self.edges[v]:
                if cap and self.level[to] < 0:
                    self.level[to] = lnxt
                    q.append(to)
        return self.level[t] >= 0

    def dfs_dinic(self, v: int, goal: int, f: int) -> int:
        if v == goal:
            return f
        
        for e in self.it[v]:
            to, cap, rev = e
            if cap and self.level[v] < self.level[to]:
                d = self.dfs_dinic(to, goal, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def Dinic(self, s: int, t: int, req: int) -> int:
        flow = 0
        while self.bfs(s, t) and req:
            *self.it, = map(iter, self.edges)
            while req:
                f = self.dfs_dinic(s, t, req)
                if f == 0:
                    break
                flow += f
                req -= f
        return flow


N, M, K = map(int, input().split())

A = [-1] * M
B = [-1] * M
C = [-1] * M
D = [-1] * M

c2eid = []
for eid in range(M):
    A[eid], B[eid], C[eid], D[eid] = map(int, input().split())
    c2eid.append((C[eid], eid))

X = [0] * M

for ci, i in sorted(c2eid):
    mf = MaxFlow(N + 2)
    mf.add_edge(0, A[i], K * N)
    mf.add_edge(0, B[i], K * N)
    for a, b, x in zip(A, B, X):
        mf.add_edge(0, a, x)
        mf.add_edge(a, b, x)
    for j in range(N):
        mf.add_edge(j + 1, N + 1, K)
    X[i] = min(D[i], mf.Dinic(0, N + 1, 1 << 60) - K - sum(X))

if sum(X) < K * (N - 1):
    print(-1)
else:
    print(sum([c * x for c, x in zip(C, X)]))
0