結果

問題 No.1678 Coin Trade (Multiple)
ユーザー chineristACchineristAC
提出日時 2021-09-10 22:56:56
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,597 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 270,380 KB
最終ジャッジ日時 2023-09-02 20:32:20
合計ジャッジ時間 29,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 195 ms
83,164 KB
testcase_01 AC 196 ms
83,044 KB
testcase_02 AC 197 ms
83,056 KB
testcase_03 AC 905 ms
126,300 KB
testcase_04 AC 3,858 ms
266,264 KB
testcase_05 AC 1,729 ms
172,352 KB
testcase_06 AC 1,362 ms
154,676 KB
testcase_07 AC 3,401 ms
251,540 KB
testcase_08 AC 2,341 ms
198,220 KB
testcase_09 AC 1,774 ms
179,164 KB
testcase_10 AC 844 ms
108,916 KB
testcase_11 AC 2,784 ms
227,448 KB
testcase_12 AC 845 ms
110,232 KB
testcase_13 TLE -
testcase_14 AC 1,812 ms
173,532 KB
testcase_15 AC 2,096 ms
190,392 KB
testcase_16 AC 753 ms
120,384 KB
testcase_17 AC 1,968 ms
180,892 KB
testcase_18 AC 222 ms
84,024 KB
testcase_19 AC 207 ms
82,772 KB
testcase_20 AC 215 ms
83,200 KB
testcase_21 AC 220 ms
84,096 KB
testcase_22 AC 218 ms
83,948 KB
testcase_23 AC 211 ms
84,092 KB
testcase_24 AC 222 ms
83,848 KB
testcase_25 AC 207 ms
82,752 KB
testcase_26 AC 217 ms
83,536 KB
testcase_27 AC 212 ms
83,892 KB
testcase_28 AC 213 ms
83,572 KB
testcase_29 AC 206 ms
82,756 KB
testcase_30 AC 210 ms
83,236 KB
testcase_31 AC 208 ms
82,760 KB
testcase_32 AC 210 ms
82,944 KB
testcase_33 AC 227 ms
84,012 KB
testcase_34 AC 221 ms
83,632 KB
testcase_35 AC 209 ms
82,896 KB
testcase_36 AC 225 ms
83,808 KB
testcase_37 AC 232 ms
83,784 KB
testcase_38 AC 218 ms
83,984 KB
testcase_39 AC 213 ms
83,200 KB
testcase_40 AC 220 ms
83,536 KB
testcase_41 AC 221 ms
83,784 KB
testcase_42 AC 228 ms
83,868 KB
testcase_43 AC 233 ms
83,868 KB
testcase_44 AC 214 ms
83,360 KB
testcase_45 AC 211 ms
82,884 KB
testcase_46 AC 231 ms
83,812 KB
testcase_47 AC 216 ms
83,604 KB
testcase_48 TLE -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import NamedTuple, Optional, List, Tuple, cast
from heapq import heappush, heappop

"""
from:https://github.com/not522/ac-library-python/blob/master/atcoder/mincostflow.py
"""
class MCFGraph:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int
        cost: int

    class _Edge:
        def __init__(self, dst: int, cap: int, cost: int) -> None:
            self.dst = dst
            self.cap = cap
            self.cost = cost
            self.rev: Optional[MCFGraph._Edge] = None

    def __init__(self, n: int) -> None:
        self._n = n
        self._g: List[List[MCFGraph._Edge]] = [[] for _ in range(n)]
        self._edges: List[MCFGraph._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int, cost: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MCFGraph._Edge(dst, cap, cost)
        re = MCFGraph._Edge(src, 0, -cost)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = cast(MCFGraph._Edge, e.rev)
        return MCFGraph.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap,
            e.cost
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def flow(self, s: int, t: int,
             flow_limit: Optional[int] = None) -> Tuple[int, int]:
        return self.slope(s, t, flow_limit)[-1]

    def slope(self, s: int, t: int,
              flow_limit: Optional[int] = None) -> List[Tuple[int, int]]:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = cast(int, sum(e.cap for e in self._g[s]))

        dual = [0] * self._n
        prev: List[Optional[Tuple[int, MCFGraph._Edge]]] = [None] * self._n

        def refine_dual() -> bool:
            pq = [(0, s)]
            visited = [False] * self._n
            dist: List[Optional[int]] = [None] * self._n
            dist[s] = 0
            while pq:
                dist_v, v = heappop(pq)
                if visited[v]:
                    continue
                visited[v] = True
                if v == t:
                    break
                dual_v = dual[v]
                for e in self._g[v]:
                    w = e.dst
                    if visited[w] or e.cap == 0:
                        continue
                    reduced_cost = e.cost - dual[w] + dual_v
                    new_dist = dist_v + reduced_cost
                    dist_w = dist[w]
                    if dist_w is None or new_dist < dist_w:
                        dist[w] = new_dist
                        prev[w] = v, e
                        heappush(pq, (new_dist, w))
            else:
                return False
            dist_t = dist[t]
            for v in range(self._n):
                if visited[v]:
                    dual[v] -= cast(int, dist_t) - cast(int, dist[v])
            return True

        flow = 0
        cost = 0
        prev_cost_per_flow: Optional[int] = None
        result = [(flow, cost)]
        while flow < flow_limit:
            if not refine_dual():
                break
            f = flow_limit - flow
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                f = min(f, e.cap)
                v = u
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                e.cap -= f
                assert e.rev is not None
                e.rev.cap += f
                v = u
            c = -dual[s]
            flow += f
            cost += f * c
            if c == prev_cost_per_flow:
                result.pop()
            result.append((flow, cost))
            prev_cost_per_flow = c
        return result

import sys,random

input = lambda :sys.stdin.buffer.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

INF_COST = 2 * 10**9

N,K = mi()
G = MCFGraph(N)
A = [0 for i in range(N)]
for i in range(N):
    A[i],M = mi()
    B = li()
    for pi in B:
        G.add_edge(pi-1,i,1,A[pi-1]-A[i]+INF_COST*(i+1-pi))
for i in range(N-1):
    G.add_edge(i,i+1,K,INF_COST)

f = G.flow(0,N-1,flow_limit=K)
print(INF_COST*K*(N-1)-f[1])
0