結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー hitonanodehitonanode
提出日時 2020-12-12 05:59:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 706 ms / 2,000 ms
コード長 2,267 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-09-19 22:31:01
合計ジャッジ時間 25,116 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 30 ms
11,008 KB
testcase_02 AC 30 ms
11,008 KB
testcase_03 AC 36 ms
11,136 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 31 ms
11,008 KB
testcase_06 AC 32 ms
11,136 KB
testcase_07 AC 466 ms
11,904 KB
testcase_08 AC 475 ms
12,032 KB
testcase_09 AC 483 ms
12,032 KB
testcase_10 AC 529 ms
12,032 KB
testcase_11 AC 78 ms
11,520 KB
testcase_12 AC 69 ms
11,520 KB
testcase_13 AC 64 ms
11,392 KB
testcase_14 AC 64 ms
11,392 KB
testcase_15 AC 382 ms
12,288 KB
testcase_16 AC 501 ms
12,416 KB
testcase_17 AC 378 ms
12,416 KB
testcase_18 AC 432 ms
12,416 KB
testcase_19 AC 44 ms
11,264 KB
testcase_20 AC 46 ms
11,264 KB
testcase_21 AC 45 ms
11,264 KB
testcase_22 AC 44 ms
11,264 KB
testcase_23 AC 47 ms
11,264 KB
testcase_24 AC 50 ms
11,392 KB
testcase_25 AC 47 ms
11,264 KB
testcase_26 AC 47 ms
11,392 KB
testcase_27 AC 45 ms
11,392 KB
testcase_28 AC 41 ms
11,392 KB
testcase_29 AC 41 ms
11,264 KB
testcase_30 AC 41 ms
11,264 KB
testcase_31 AC 47 ms
11,264 KB
testcase_32 AC 51 ms
11,392 KB
testcase_33 AC 45 ms
11,136 KB
testcase_34 AC 47 ms
11,264 KB
testcase_35 AC 41 ms
11,264 KB
testcase_36 AC 41 ms
11,392 KB
testcase_37 AC 41 ms
11,136 KB
testcase_38 AC 40 ms
11,264 KB
testcase_39 AC 46 ms
11,392 KB
testcase_40 AC 45 ms
11,392 KB
testcase_41 AC 44 ms
11,264 KB
testcase_42 AC 44 ms
11,136 KB
testcase_43 AC 431 ms
11,776 KB
testcase_44 AC 475 ms
11,648 KB
testcase_45 AC 582 ms
11,776 KB
testcase_46 AC 635 ms
11,776 KB
testcase_47 AC 184 ms
12,160 KB
testcase_48 AC 179 ms
12,160 KB
testcase_49 AC 157 ms
12,288 KB
testcase_50 AC 148 ms
12,160 KB
testcase_51 AC 492 ms
12,288 KB
testcase_52 AC 490 ms
12,416 KB
testcase_53 AC 706 ms
12,160 KB
testcase_54 AC 631 ms
12,160 KB
testcase_55 AC 542 ms
12,032 KB
testcase_56 AC 560 ms
12,032 KB
testcase_57 AC 544 ms
12,032 KB
testcase_58 AC 637 ms
12,032 KB
testcase_59 AC 526 ms
11,904 KB
testcase_60 AC 444 ms
11,904 KB
testcase_61 AC 626 ms
12,032 KB
testcase_62 AC 594 ms
12,032 KB
testcase_63 AC 583 ms
12,032 KB
testcase_64 AC 459 ms
12,032 KB
testcase_65 AC 495 ms
12,032 KB
testcase_66 AC 587 ms
11,904 KB
testcase_67 AC 598 ms
11,904 KB
testcase_68 AC 488 ms
12,032 KB
testcase_69 AC 506 ms
12,032 KB
testcase_70 AC 477 ms
12,288 KB
testcase_71 AC 543 ms
12,416 KB
testcase_72 AC 648 ms
12,416 KB
testcase_73 AC 434 ms
12,288 KB
testcase_74 AC 526 ms
12,416 KB
testcase_75 AC 57 ms
11,392 KB
testcase_76 AC 63 ms
11,392 KB
testcase_77 AC 59 ms
11,392 KB
testcase_78 AC 414 ms
12,288 KB
testcase_79 AC 388 ms
12,544 KB
testcase_80 AC 443 ms
12,288 KB
testcase_81 AC 191 ms
12,288 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