結果

問題 No.1678 Coin Trade (Multiple)
ユーザー wolgnikwolgnik
提出日時 2021-09-10 23:35:31
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,088 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 86,856 KB
実行使用メモリ 226,904 KB
最終ジャッジ日時 2023-09-02 22:50:40
合計ジャッジ時間 8,024 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
71,416 KB
testcase_01 AC 82 ms
71,180 KB
testcase_02 AC 83 ms
71,132 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N, K = map(int, input().split())

from heapq import heappop, heappush, heapify
class MinCostFlow():
  def __init__(self, n):
    self.n = n
    self.graph = [[] for _ in range(n)]
    self.pos = []
  def add_edge(self, fr, to, cap, cost):
    m = len(self.pos)
    self.pos.append((fr, len(self.graph[fr])))
    self.graph[fr].append([to, len(self.graph[to]), cap, cost])
    self.graph[to].append([fr, len(self.graph[fr]) - 1, 0, -cost])
    return m
  def get_edge(self, idx):
    to, rev, cap, cost = self.graph[self.pos[idx][0]][self.pos[idx][1]]
    rev_to, rev_rev, rev_cap, rev_cost = self.graph[to][rev]
    return self.pos[idx][0], to, cap + rev_cap, rev_cap, cost
  def edges(self):
    for i in range(len(self.pos)):
      yield self.get_edge(i)
  def dual_ref(self, s, t):
    dist = [2**63 - 1] * self.n
    dist[s] = 0
    vis = [0] * self.n
    self.pv = [-1] * self.n
    self.pe = [-1] * self.n
    queue = []
    heappush(queue, (0, s))
    while queue:
      k, v = heappop(queue)
      if vis[v]: continue
      vis[v] = True
      if v == t: break
      for i in range(len(self.graph[v])):
        to, rev, cap, cost = self.graph[v][i]
        if vis[to] or cap == 0: continue
        cost += self.dual[v] - self.dual[to]
        if dist[to] - dist[v] > cost:
          dist[to] = dist[v] + cost
          self.pv[to] = v
          self.pe[to] = i
          heappush(queue, (dist[to], to))
    if not vis[t]: return False
    for v in range(self.n):
      if not vis[v]: continue
      self.dual[v] -= dist[t] - dist[v]
    return True
  def flow(self, s, t): return self.flow_with_limit(s, t, 2**63 - 1)
  def flow_with_limit(self, s, t, limit): return self.slope_with_limit(s, t, limit)[-1]
  def slope(self, s, t): return self.slope_with_limit(s, t, 2**63 - 1)
  def slope_with_limit(self, s, t, limit):
    flow = 0
    cost = 0
    prev_cost = -1
    res = [(flow, cost)]
    self.dual = [0] * self.n
    while flow < limit:
      if not self.dual_ref(s, t): break
      c = limit - flow
      v = t
      while v != s:
        c = min(c, self.graph[self.pv[v]][self.pe[v]][2])
        v = self.pv[v]
      v = t
      while v != s:
        to, rev, cap, _ = self.graph[self.pv[v]][self.pe[v]]
        self.graph[self.pv[v]][self.pe[v]][2] -= c
        self.graph[v][rev][2] += c
        v = self.pv[v]
      d = -self.dual[s]
      flow += c
      cost += c * d
      if prev_cost == d: res.pop()
      res.append((flow, cost))
      prev_cost = cost
    return res

mcf = MinCostFlow(N + 4)
s = N + 2
t = N + 3
res = 0
a = [0] * (N + 1)
c = 0
for i in range(1, N + 1):
  x, m = map(int, input().split())
  b = list(map(int, input().split()))
  a[i] = x
  for j in b:
    if x - a[j] > 0:
      mcf.add_edge(i + 1, j - 1, 1, x - a[j])
      mcf.add_edge(s, i + 1, 1, 0)
      mcf.add_edge(j - 1, t, 1, 0)
      res -= (x - a[j])
      c += 1

inf = 10 ** 6
for i in range(1, N + 1): mcf.add_edge(i, i + 1, inf, 0)
mcf.add_edge(s, 0, K, 0)
mcf.add_edge(N + 1, t, K, 0)

res += mcf.flow_with_limit(s, t, K + c)[1]
print(-res)
0