結果

問題 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  
実行時間 659 ms / 2,000 ms
コード長 2,267 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 12,204 KB
実行使用メモリ 11,540 KB
最終ジャッジ日時 2023-10-20 02:43:13
合計ジャッジ時間 23,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,396 KB
testcase_01 AC 30 ms
10,396 KB
testcase_02 AC 31 ms
10,408 KB
testcase_03 AC 35 ms
10,496 KB
testcase_04 AC 30 ms
10,392 KB
testcase_05 AC 29 ms
10,392 KB
testcase_06 AC 29 ms
10,412 KB
testcase_07 AC 427 ms
11,380 KB
testcase_08 AC 445 ms
11,380 KB
testcase_09 AC 455 ms
11,380 KB
testcase_10 AC 494 ms
11,380 KB
testcase_11 AC 71 ms
10,872 KB
testcase_12 AC 63 ms
10,868 KB
testcase_13 AC 60 ms
10,832 KB
testcase_14 AC 61 ms
10,832 KB
testcase_15 AC 356 ms
11,320 KB
testcase_16 AC 466 ms
11,336 KB
testcase_17 AC 354 ms
11,304 KB
testcase_18 AC 402 ms
11,304 KB
testcase_19 AC 44 ms
10,728 KB
testcase_20 AC 44 ms
10,732 KB
testcase_21 AC 43 ms
10,728 KB
testcase_22 AC 43 ms
10,728 KB
testcase_23 AC 46 ms
10,776 KB
testcase_24 AC 48 ms
10,776 KB
testcase_25 AC 46 ms
10,744 KB
testcase_26 AC 46 ms
10,744 KB
testcase_27 AC 40 ms
10,728 KB
testcase_28 AC 39 ms
10,728 KB
testcase_29 AC 40 ms
10,716 KB
testcase_30 AC 39 ms
10,724 KB
testcase_31 AC 44 ms
10,772 KB
testcase_32 AC 47 ms
10,776 KB
testcase_33 AC 44 ms
10,744 KB
testcase_34 AC 46 ms
10,744 KB
testcase_35 AC 41 ms
10,728 KB
testcase_36 AC 40 ms
10,728 KB
testcase_37 AC 41 ms
10,716 KB
testcase_38 AC 40 ms
10,716 KB
testcase_39 AC 44 ms
10,776 KB
testcase_40 AC 43 ms
10,776 KB
testcase_41 AC 43 ms
10,744 KB
testcase_42 AC 43 ms
10,744 KB
testcase_43 AC 398 ms
11,348 KB
testcase_44 AC 437 ms
11,348 KB
testcase_45 AC 539 ms
11,364 KB
testcase_46 AC 594 ms
11,348 KB
testcase_47 AC 174 ms
11,492 KB
testcase_48 AC 169 ms
11,492 KB
testcase_49 AC 149 ms
11,456 KB
testcase_50 AC 137 ms
11,456 KB
testcase_51 AC 448 ms
11,540 KB
testcase_52 AC 445 ms
11,540 KB
testcase_53 AC 659 ms
11,524 KB
testcase_54 AC 585 ms
11,540 KB
testcase_55 AC 511 ms
11,476 KB
testcase_56 AC 527 ms
11,476 KB
testcase_57 AC 514 ms
11,476 KB
testcase_58 AC 608 ms
11,476 KB
testcase_59 AC 510 ms
11,476 KB
testcase_60 AC 426 ms
11,380 KB
testcase_61 AC 589 ms
11,380 KB
testcase_62 AC 563 ms
11,380 KB
testcase_63 AC 547 ms
11,380 KB
testcase_64 AC 428 ms
11,380 KB
testcase_65 AC 465 ms
11,392 KB
testcase_66 AC 556 ms
11,400 KB
testcase_67 AC 573 ms
11,416 KB
testcase_68 AC 466 ms
11,392 KB
testcase_69 AC 482 ms
11,400 KB
testcase_70 AC 448 ms
11,320 KB
testcase_71 AC 507 ms
11,320 KB
testcase_72 AC 608 ms
11,320 KB
testcase_73 AC 410 ms
11,324 KB
testcase_74 AC 499 ms
11,320 KB
testcase_75 AC 54 ms
10,784 KB
testcase_76 AC 60 ms
10,784 KB
testcase_77 AC 56 ms
10,784 KB
testcase_78 AC 384 ms
11,316 KB
testcase_79 AC 365 ms
11,320 KB
testcase_80 AC 415 ms
11,316 KB
testcase_81 AC 178 ms
11,320 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