結果

問題 No.1541 ゅゅさんのテスト勉強
ユーザー qibqib
提出日時 2023-01-03 17:33:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,275 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 77,732 KB
最終ジャッジ日時 2023-08-18 00:42:39
合計ジャッジ時間 6,373 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,636 KB
testcase_01 AC 94 ms
71,508 KB
testcase_02 AC 95 ms
71,180 KB
testcase_03 AC 96 ms
71,672 KB
testcase_04 AC 96 ms
71,692 KB
testcase_05 AC 95 ms
71,688 KB
testcase_06 AC 95 ms
71,688 KB
testcase_07 AC 95 ms
71,772 KB
testcase_08 AC 126 ms
77,492 KB
testcase_09 AC 97 ms
71,512 KB
testcase_10 AC 98 ms
71,556 KB
testcase_11 AC 98 ms
71,504 KB
testcase_12 AC 98 ms
71,400 KB
testcase_13 AC 98 ms
71,420 KB
testcase_14 AC 96 ms
71,396 KB
testcase_15 AC 96 ms
71,696 KB
testcase_16 AC 97 ms
71,704 KB
testcase_17 AC 96 ms
71,400 KB
testcase_18 AC 97 ms
71,516 KB
testcase_19 AC 109 ms
76,488 KB
testcase_20 AC 106 ms
75,820 KB
testcase_21 AC 109 ms
76,700 KB
testcase_22 AC 108 ms
76,024 KB
testcase_23 AC 110 ms
75,996 KB
testcase_24 AC 109 ms
76,312 KB
testcase_25 AC 107 ms
76,176 KB
testcase_26 AC 108 ms
76,136 KB
testcase_27 AC 111 ms
76,792 KB
testcase_28 AC 111 ms
76,236 KB
testcase_29 AC 121 ms
77,360 KB
testcase_30 AC 122 ms
77,732 KB
testcase_31 AC 121 ms
77,728 KB
testcase_32 AC 121 ms
77,504 KB
testcase_33 AC 122 ms
77,392 KB
testcase_34 AC 122 ms
77,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)

class Dinic:
  def __init__(self, n, flow_limit):
    self.vertices = n
    self.flow_limit = flow_limit
    self.links = [[] for _ in range(n)]
    self.level = None
    self.progress = None

  def add_link(self, f, t, cap):
    from_id = len(self.links[f])
    to_id = len(self.links[t])
    if f == t:
      to_id += 1
    self.links[f].append([cap, t, to_id])
    self.links[t].append([0, f, from_id])

  def bfs(self, src, dst):
    level = [-1 for _ in range(self.vertices)]
    level[src] = 0
    dq = deque([src])
    while len(dq) > 0:
      cur = dq.popleft()
      for cap, nxt, _ in self.links[cur]:
        if cap > 0 and level[nxt] < 0:
          level[nxt] = level[cur] + 1
          dq.append(nxt)

    self.level = level
    return level[dst] != -1

  def dfs(self, cur, src, up):
    if cur == src:
      return up

    ans = 0
    lv_cur = self.level[cur]
    for i in range(self.progress[cur], len(self.links[cur])):
      self.progress[cur] += 1
      cap, nxt, rev = self.links[cur][i]
      if self.level[nxt] >= lv_cur or self.links[nxt][rev][0] == 0:
        continue

      d = self.dfs(nxt, src, min(up - ans, self.links[nxt][rev][0]))
      if d <= 0:
        continue

      self.links[cur][i][0] += d
      self.links[nxt][rev][0] -= d
      ans += d
      if ans == up:
        break

    return ans

  def max_flow(self, src, dst):
    ans = 0
    while ans < self.flow_limit:
      if not self.bfs(src, dst):
        break
      self.progress = [-1 for _ in range(self.vertices)]

      while ans < self.flow_limit:
        f = self.dfs(dst, src, self.flow_limit - ans)
        if f <= 0:
          break
        ans += f

    return ans

INF = 10 ** 13

n, m = map(int, input().split())

src = n
dst = n + 1

mf = Dinic(n + 2, INF)

ans = m * n
for u in range(n):
  k, c = map(int, input().split())
  if k == 0:
    mf.add_link(u, dst, c)
    mf.add_link(src, u, m)
  else:
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    sum_b = sum(b)
    ans += sum_b
    mf.add_link(u, dst, c)
    mf.add_link(src, u, m + sum_b)
  
    for j in range(k):
      v = a[j] - 1
      mf.add_link(u, v, b[j])

ans -= mf.max_flow(src, dst)
print(ans)
0