結果

問題 No.1678 Coin Trade (Multiple)
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-12-09 23:06:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,974 ms / 5,000 ms
コード長 3,781 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 264,492 KB
最終ジャッジ日時 2023-09-24 15:19:16
合計ジャッジ時間 63,978 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,204 KB
testcase_01 AC 76 ms
71,328 KB
testcase_02 AC 75 ms
71,336 KB
testcase_03 AC 528 ms
102,864 KB
testcase_04 AC 1,956 ms
184,512 KB
testcase_05 AC 968 ms
134,324 KB
testcase_06 AC 739 ms
120,892 KB
testcase_07 AC 1,684 ms
165,676 KB
testcase_08 AC 1,242 ms
141,348 KB
testcase_09 AC 1,039 ms
137,516 KB
testcase_10 AC 409 ms
90,280 KB
testcase_11 AC 1,509 ms
157,844 KB
testcase_12 AC 370 ms
92,008 KB
testcase_13 AC 2,996 ms
245,008 KB
testcase_14 AC 1,021 ms
127,000 KB
testcase_15 AC 1,103 ms
138,944 KB
testcase_16 AC 466 ms
104,240 KB
testcase_17 AC 1,054 ms
141,336 KB
testcase_18 AC 85 ms
76,092 KB
testcase_19 AC 77 ms
71,308 KB
testcase_20 AC 77 ms
71,276 KB
testcase_21 AC 80 ms
75,460 KB
testcase_22 AC 84 ms
75,784 KB
testcase_23 AC 77 ms
71,208 KB
testcase_24 AC 89 ms
76,080 KB
testcase_25 AC 78 ms
71,196 KB
testcase_26 AC 85 ms
76,528 KB
testcase_27 AC 77 ms
70,952 KB
testcase_28 AC 83 ms
76,384 KB
testcase_29 AC 77 ms
71,148 KB
testcase_30 AC 82 ms
75,468 KB
testcase_31 AC 76 ms
71,452 KB
testcase_32 AC 75 ms
71,140 KB
testcase_33 AC 87 ms
76,228 KB
testcase_34 AC 79 ms
70,956 KB
testcase_35 AC 79 ms
71,340 KB
testcase_36 AC 90 ms
76,232 KB
testcase_37 AC 93 ms
76,020 KB
testcase_38 AC 78 ms
71,196 KB
testcase_39 AC 78 ms
71,304 KB
testcase_40 AC 81 ms
75,464 KB
testcase_41 AC 85 ms
76,176 KB
testcase_42 AC 89 ms
75,724 KB
testcase_43 AC 97 ms
76,664 KB
testcase_44 AC 78 ms
70,932 KB
testcase_45 AC 76 ms
70,988 KB
testcase_46 AC 89 ms
76,272 KB
testcase_47 AC 78 ms
71,348 KB
testcase_48 AC 3,849 ms
262,620 KB
testcase_49 AC 3,957 ms
263,420 KB
testcase_50 AC 3,906 ms
262,520 KB
testcase_51 AC 3,827 ms
262,608 KB
testcase_52 AC 3,868 ms
263,112 KB
testcase_53 AC 3,966 ms
264,492 KB
testcase_54 AC 3,767 ms
262,676 KB
testcase_55 AC 3,832 ms
263,096 KB
testcase_56 AC 3,974 ms
263,032 KB
testcase_57 AC 3,961 ms
263,380 KB
testcase_58 AC 2,592 ms
262,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import heappush, heappop

class mincostflow:

    class edge:
        def __init__(self, from_, to, cap, flow, cost):
            self.from_ = from_
            self.to = to
            self.cap = cap
            self.flow = flow
            self.cost = cost
    class _edge:
        def __init__(self, to, rev, cap, cost):
            self.to = to
            self.rev = rev
            self.cap = cap
            self.cost = cost

    def __init__(self, n):
        self.n = n
        self.pos = []
        self.g = [[] for i in range(n)]

    def add_edge(self, from_, to, cap, cost):
        m = len(self.pos)
        self.pos.append((from_, len(self.g[from_])))
        self.g[from_].append(self.__class__._edge(to, len(self.g[to]), cap, cost))
        self.g[to].append(self.__class__._edge(from_, len(self.g[from_])-1, 0, -cost))
        return m

    def get_edge(self, i):
        _e = self.g[self.pos[i][0]][self.pos[i][1]]
        _re = self.g[_e.to][_e.rev]
        return self.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost)

    def edges(self):
        result = []
        for i in range(len(self.pos)):
            result.append(self.get_edge(i))
        return result

    def slope(self, s, t, flow_limit=10**20, inf=10**20):
        dual = [0]*self.n
        dist = [inf]*self.n
        pv = [-1]*self.n
        pe = [-1]*self.n
        vis = [False]*self.n

        def _dual_ref():
            nonlocal dual, dist, pv, pe, vis
            dist = [inf]*self.n
            pv = [-1]*self.n
            pe = [-1]*self.n
            vis = [False]*self.n

            que = [(0,s)]
            dist[s] = 0
            while que:
                _,v = heappop(que)
                if vis[v]:
                    continue
                vis[v] = True

                if v == t:
                    break
                for i in range(len(self.g[v])):
                    e = self.g[v][i]
                    if vis[e.to] or e.cap == 0:
                        continue
                    cost = e.cost - dual[e.to] + dual[v]
                    if dist[e.to] > dist[v] + cost:
                        dist[e.to] = dist[v] + cost
                        pv[e.to] = v
                        pe[e.to] = i
                        heappush(que, (dist[e.to],e.to))
            if not vis[t]:
                return False

            for v in range(self.n):
                if not vis[v]:
                    continue
                dual[v] -= dist[t] - dist[v]
            return True
        
        flow = 0
        cost = 0
        prev_cost = -1

        result = [(flow, cost)]
        while flow < flow_limit:
            if not _dual_ref():
                break
            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[pv[v]][pe[v]].cap)
                v = pv[v]

            v = t
            while v != s:
                e = self.g[pv[v]][pe[v]]
                e.cap -= c
                self.g[v][e.rev].cap += c
                v = pv[v]
            
            d = -dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                result.pop()
            result.append((flow, cost))
            prev_cost = cost
        return result

    def flow(self, s, t, flow_limit=10**20):
        return self.slope(s, t, flow_limit)[-1]
    

n,k = map(int,input().split())
g = mincostflow(n)
A = []
M = 10**9
for i in range(n):
    a,m = map(int,input().split())
    A.append(a)
    B = [int(x)-1 for x in input().split()]
    for b in B:
        if A[b] < a:
            g.add_edge(b,i,1,A[b]-a+(i-b)*M)
    
    if i:
        g.add_edge(i-1,i,k,M)

ans = g.flow(0,n-1,k)
print((n-1)*k*M-ans[1])
0