結果

問題 No.1316 Maximum Minimum Spanning Tree
ユーザー optopt
提出日時 2020-12-12 22:02:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 619 ms / 2,000 ms
コード長 2,331 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 12,204 KB
実行使用メモリ 11,928 KB
最終ジャッジ日時 2023-10-20 02:41:10
合計ジャッジ時間 24,407 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,388 KB
testcase_01 AC 31 ms
10,388 KB
testcase_02 AC 32 ms
10,392 KB
testcase_03 AC 37 ms
10,476 KB
testcase_04 AC 31 ms
10,388 KB
testcase_05 AC 32 ms
10,388 KB
testcase_06 AC 34 ms
10,396 KB
testcase_07 AC 441 ms
11,392 KB
testcase_08 AC 436 ms
11,392 KB
testcase_09 AC 456 ms
11,376 KB
testcase_10 AC 523 ms
11,392 KB
testcase_11 AC 70 ms
10,868 KB
testcase_12 AC 63 ms
10,860 KB
testcase_13 AC 60 ms
10,824 KB
testcase_14 AC 66 ms
10,824 KB
testcase_15 AC 351 ms
11,908 KB
testcase_16 AC 455 ms
11,928 KB
testcase_17 AC 344 ms
11,884 KB
testcase_18 AC 400 ms
11,892 KB
testcase_19 AC 45 ms
10,716 KB
testcase_20 AC 49 ms
10,716 KB
testcase_21 AC 44 ms
10,704 KB
testcase_22 AC 44 ms
10,700 KB
testcase_23 AC 48 ms
10,760 KB
testcase_24 AC 49 ms
10,760 KB
testcase_25 AC 46 ms
10,724 KB
testcase_26 AC 47 ms
10,724 KB
testcase_27 AC 41 ms
10,716 KB
testcase_28 AC 41 ms
10,716 KB
testcase_29 AC 41 ms
10,704 KB
testcase_30 AC 41 ms
10,704 KB
testcase_31 AC 45 ms
10,756 KB
testcase_32 AC 49 ms
10,760 KB
testcase_33 AC 45 ms
10,724 KB
testcase_34 AC 47 ms
10,724 KB
testcase_35 AC 41 ms
10,704 KB
testcase_36 AC 45 ms
10,704 KB
testcase_37 AC 41 ms
10,688 KB
testcase_38 AC 41 ms
10,688 KB
testcase_39 AC 46 ms
10,760 KB
testcase_40 AC 44 ms
10,760 KB
testcase_41 AC 44 ms
10,724 KB
testcase_42 AC 45 ms
10,724 KB
testcase_43 AC 416 ms
11,424 KB
testcase_44 AC 443 ms
11,424 KB
testcase_45 AC 566 ms
11,440 KB
testcase_46 AC 568 ms
11,424 KB
testcase_47 AC 181 ms
11,484 KB
testcase_48 AC 177 ms
11,480 KB
testcase_49 AC 156 ms
11,440 KB
testcase_50 AC 144 ms
11,444 KB
testcase_51 AC 489 ms
11,580 KB
testcase_52 AC 478 ms
11,580 KB
testcase_53 AC 619 ms
11,580 KB
testcase_54 AC 584 ms
11,584 KB
testcase_55 AC 528 ms
11,472 KB
testcase_56 AC 503 ms
11,472 KB
testcase_57 AC 498 ms
11,456 KB
testcase_58 AC 583 ms
11,472 KB
testcase_59 AC 534 ms
11,456 KB
testcase_60 AC 436 ms
11,376 KB
testcase_61 AC 550 ms
11,392 KB
testcase_62 AC 588 ms
11,376 KB
testcase_63 AC 583 ms
11,376 KB
testcase_64 AC 433 ms
11,376 KB
testcase_65 AC 470 ms
11,476 KB
testcase_66 AC 553 ms
11,476 KB
testcase_67 AC 529 ms
11,476 KB
testcase_68 AC 439 ms
11,460 KB
testcase_69 AC 445 ms
11,480 KB
testcase_70 AC 458 ms
11,900 KB
testcase_71 AC 496 ms
11,916 KB
testcase_72 AC 582 ms
11,924 KB
testcase_73 AC 424 ms
11,900 KB
testcase_74 AC 491 ms
11,908 KB
testcase_75 AC 54 ms
10,768 KB
testcase_76 AC 59 ms
10,772 KB
testcase_77 AC 56 ms
10,768 KB
testcase_78 AC 373 ms
11,912 KB
testcase_79 AC 343 ms
11,908 KB
testcase_80 AC 377 ms
11,912 KB
testcase_81 AC 181 ms
11,868 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):
        g.add_edge(s, a2, y[j])
        g.add_edge(a2, b2, y[j])
    for v in range(1, n+1):
        g.add_edge(v, t, k)
    g.add_edge(s, a, k*n)
    g.add_edge(s, b, k*n)
    f = g.flow(s, t)
    z = f - k - ysum
    y[i] = min(z, d)
    ysum += y[i]
    ans += c * y[i]

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