結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー hitonanodehitonanode
提出日時 2020-12-12 22:09:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 2,267 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 80,240 KB
最終ジャッジ日時 2024-09-19 22:30:31
合計ジャッジ時間 18,684 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,760 KB
testcase_01 AC 40 ms
54,016 KB
testcase_02 AC 41 ms
54,656 KB
testcase_03 AC 82 ms
76,436 KB
testcase_04 AC 40 ms
54,016 KB
testcase_05 AC 41 ms
54,144 KB
testcase_06 AC 41 ms
54,656 KB
testcase_07 AC 299 ms
78,792 KB
testcase_08 AC 306 ms
79,260 KB
testcase_09 AC 296 ms
78,708 KB
testcase_10 AC 316 ms
79,120 KB
testcase_11 AC 158 ms
78,452 KB
testcase_12 AC 149 ms
77,824 KB
testcase_13 AC 141 ms
77,688 KB
testcase_14 AC 140 ms
77,568 KB
testcase_15 AC 272 ms
79,124 KB
testcase_16 AC 298 ms
79,156 KB
testcase_17 AC 262 ms
78,776 KB
testcase_18 AC 289 ms
79,280 KB
testcase_19 AC 106 ms
76,928 KB
testcase_20 AC 108 ms
77,488 KB
testcase_21 AC 109 ms
77,440 KB
testcase_22 AC 108 ms
77,696 KB
testcase_23 AC 114 ms
77,696 KB
testcase_24 AC 125 ms
78,104 KB
testcase_25 AC 113 ms
77,404 KB
testcase_26 AC 122 ms
77,356 KB
testcase_27 AC 96 ms
76,752 KB
testcase_28 AC 97 ms
76,684 KB
testcase_29 AC 98 ms
76,912 KB
testcase_30 AC 96 ms
76,672 KB
testcase_31 AC 115 ms
77,464 KB
testcase_32 AC 114 ms
77,576 KB
testcase_33 AC 106 ms
77,456 KB
testcase_34 AC 118 ms
77,416 KB
testcase_35 AC 96 ms
76,728 KB
testcase_36 AC 96 ms
76,876 KB
testcase_37 AC 100 ms
76,796 KB
testcase_38 AC 104 ms
76,932 KB
testcase_39 AC 113 ms
77,416 KB
testcase_40 AC 114 ms
77,528 KB
testcase_41 AC 104 ms
77,020 KB
testcase_42 AC 106 ms
76,988 KB
testcase_43 AC 269 ms
78,584 KB
testcase_44 AC 275 ms
78,484 KB
testcase_45 AC 304 ms
78,780 KB
testcase_46 AC 342 ms
79,144 KB
testcase_47 AC 174 ms
78,064 KB
testcase_48 AC 171 ms
77,804 KB
testcase_49 AC 160 ms
77,764 KB
testcase_50 AC 155 ms
77,740 KB
testcase_51 AC 283 ms
78,752 KB
testcase_52 AC 288 ms
79,080 KB
testcase_53 AC 363 ms
79,516 KB
testcase_54 AC 333 ms
79,688 KB
testcase_55 AC 307 ms
78,684 KB
testcase_56 AC 314 ms
78,792 KB
testcase_57 AC 317 ms
78,708 KB
testcase_58 AC 328 ms
79,384 KB
testcase_59 AC 303 ms
79,752 KB
testcase_60 AC 281 ms
79,776 KB
testcase_61 AC 350 ms
79,312 KB
testcase_62 AC 329 ms
79,572 KB
testcase_63 AC 342 ms
79,592 KB
testcase_64 AC 279 ms
79,008 KB
testcase_65 AC 324 ms
79,332 KB
testcase_66 AC 331 ms
79,512 KB
testcase_67 AC 340 ms
79,120 KB
testcase_68 AC 293 ms
79,032 KB
testcase_69 AC 326 ms
79,620 KB
testcase_70 AC 301 ms
79,268 KB
testcase_71 AC 314 ms
78,872 KB
testcase_72 AC 367 ms
80,240 KB
testcase_73 AC 273 ms
78,860 KB
testcase_74 AC 312 ms
79,272 KB
testcase_75 AC 119 ms
77,812 KB
testcase_76 AC 130 ms
77,952 KB
testcase_77 AC 126 ms
77,944 KB
testcase_78 AC 273 ms
78,708 KB
testcase_79 AC 261 ms
79,964 KB
testcase_80 AC 287 ms
79,128 KB
testcase_81 AC 191 ms
78,036 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