結果

問題 No.2713 Just Solitaire
ユーザー 👑 timitimi
提出日時 2024-04-06 19:35:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,986 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 77,752 KB
最終ジャッジ日時 2024-04-06 19:35:15
合計ジャッジ時間 4,079 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,616 KB
testcase_01 AC 42 ms
55,616 KB
testcase_02 AC 45 ms
55,616 KB
testcase_03 AC 50 ms
61,408 KB
testcase_04 AC 66 ms
70,984 KB
testcase_05 AC 45 ms
55,616 KB
testcase_06 AC 71 ms
72,880 KB
testcase_07 AC 48 ms
61,152 KB
testcase_08 AC 42 ms
55,616 KB
testcase_09 AC 45 ms
55,616 KB
testcase_10 AC 45 ms
55,616 KB
testcase_11 AC 58 ms
65,984 KB
testcase_12 AC 44 ms
55,616 KB
testcase_13 AC 69 ms
73,016 KB
testcase_14 AC 71 ms
68,756 KB
testcase_15 AC 42 ms
55,616 KB
testcase_16 AC 44 ms
55,616 KB
testcase_17 AC 63 ms
68,756 KB
testcase_18 AC 43 ms
55,616 KB
testcase_19 AC 74 ms
74,296 KB
testcase_20 AC 42 ms
55,616 KB
testcase_21 AC 43 ms
55,616 KB
testcase_22 AC 44 ms
55,616 KB
testcase_23 AC 61 ms
68,752 KB
testcase_24 AC 66 ms
72,848 KB
testcase_25 AC 87 ms
77,352 KB
testcase_26 AC 70 ms
73,008 KB
testcase_27 AC 82 ms
77,460 KB
testcase_28 AC 88 ms
77,484 KB
testcase_29 AC 73 ms
74,384 KB
testcase_30 AC 89 ms
77,752 KB
testcase_31 AC 65 ms
72,848 KB
testcase_32 AC 90 ms
77,604 KB
testcase_33 AC 85 ms
77,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
from collections import deque

class Dinic:
    def __init__(self, n):
        self.n = n
        self.links = [[] for _ in range(n)]
        self.depth = None
        self.progress = None
 
    def add_link(self, _from, to, cap):
        self.links[_from].append([cap, to, len(self.links[to])])
        self.links[to].append([0, _from, len(self.links[_from]) - 1])
 
    def bfs(self, s):
        depth = [-1] * self.n
        depth[s] = 0
        q = deque([s])
        while q:
            v = q.popleft()
            for cap, to, rev in self.links[v]:
                if cap > 0 and depth[to] < 0:
                    depth[to] = depth[v] + 1
                    q.append(to)
        self.depth = depth
 
    def dfs(self, v, t, flow):
        if v == t:
            return flow
        links_v = self.links[v]
        for i in range(self.progress[v], len(links_v)):
            self.progress[v] = i
            cap, to, rev = link = links_v[i]
            if cap == 0 or self.depth[v] >= self.depth[to]:
                continue
            d = self.dfs(to, t, min(flow, cap))
            if d == 0:
                continue
            link[0] -= d
            self.links[to][rev][0] += d
            return d
        return 0
 
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.depth[t] < 0:
                return flow
            self.progress = [0] * self.n
            current_flow = self.dfs(s, t, float('inf'))
            while current_flow > 0:
                flow += current_flow
                current_flow = self.dfs(s, t, float('inf'))
 
# 使い方
mf = Dinic(N+M+10)
for i in range(N):
  mf.add_link(0,1+i,A[i])

for i in range(M):
  mf.add_link(N+1+i,N+M+1,B[i])
  AA=list(map(int, input().split()))
  BB=AA[1:]
  for b in BB:
    mf.add_link(b,N+1+i,10**20)
  
ans=mf.max_flow(0,N+M+1)
print(sum(B)-ans)
0