結果

問題 No.2713 Just Solitaire
ユーザー 👑 rin204rin204
提出日時 2024-04-07 14:40:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 4,012 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 78,336 KB
最終ジャッジ日時 2024-10-01 04:30:24
合計ジャッジ時間 3,605 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,060 KB
testcase_01 AC 46 ms
56,128 KB
testcase_02 AC 49 ms
56,660 KB
testcase_03 AC 84 ms
76,300 KB
testcase_04 AC 74 ms
71,976 KB
testcase_05 AC 58 ms
64,584 KB
testcase_06 AC 72 ms
71,752 KB
testcase_07 AC 63 ms
67,380 KB
testcase_08 AC 45 ms
55,968 KB
testcase_09 AC 50 ms
62,316 KB
testcase_10 AC 58 ms
64,744 KB
testcase_11 AC 66 ms
69,884 KB
testcase_12 AC 55 ms
63,880 KB
testcase_13 AC 70 ms
71,376 KB
testcase_14 AC 66 ms
70,140 KB
testcase_15 AC 47 ms
56,672 KB
testcase_16 AC 46 ms
56,064 KB
testcase_17 AC 90 ms
77,492 KB
testcase_18 AC 47 ms
56,340 KB
testcase_19 AC 74 ms
71,908 KB
testcase_20 AC 45 ms
55,440 KB
testcase_21 AC 47 ms
55,644 KB
testcase_22 AC 52 ms
62,184 KB
testcase_23 AC 74 ms
73,772 KB
testcase_24 AC 78 ms
75,380 KB
testcase_25 AC 89 ms
78,084 KB
testcase_26 AC 90 ms
78,200 KB
testcase_27 AC 93 ms
77,912 KB
testcase_28 AC 90 ms
78,216 KB
testcase_29 AC 90 ms
78,148 KB
testcase_30 AC 90 ms
78,240 KB
testcase_31 AC 90 ms
78,200 KB
testcase_32 AC 94 ms
78,336 KB
testcase_33 AC 92 ms
77,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

# from dataclasses import dataclass


class mf_graph:
    # @dataclass
    class edge:
        # from_: int
        # to: int
        # cap: int
        # flow: int
        def __init__(self, from_, to, cap, flow):
            self.from_ = from_
            self.to = to
            self.cap = cap
            self.flow = flow

    # @dataclass
    class _edge:
        # to: int
        # rev: int
        # cap: int
        def __init__(self, to, rev, cap):
            self.to = to
            self.rev = rev
            self.cap = cap

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

    def add_edge(self, from_, to, cap):
        m = len(self.pos)
        self.pos.append((from_, len(self.G[from_])))
        from_id = len(self.G[from_])
        to_id = len(self.G[to])
        if from_ == to:
            to_id += 1

        self.G[from_].append(mf_graph._edge(to, to_id, cap))
        self.G[to].append(mf_graph._edge(from_, from_id, 0))
        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 mf_graph.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap)

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

        return result

    def change_edge(self, i, new_cap, new_flow):
        _e = self.G[self.pos[i][0]][self.pos[i][1]]
        self.G[_e.to][_e.rev].cap = new_flow
        self.G[self.pos[i][0]][self.pos[i][1]].cap = new_cap - new_flow

    def flow(self, s, t, flow_limit=1 << 60):
        level = []
        iter = []
        que = deque()

        def bfs():
            nonlocal level
            level = [-1] * self.n
            level[s] = 0
            que.clear()
            que.append(s)
            while que:
                v = que.popleft()
                for e in self.G[v]:
                    if e.cap == 0 or level[e.to] >= 0:
                        continue
                    level[e.to] = level[v] + 1
                    if e.to == t:
                        return
                    que.append(e.to)

        def dfs(v, up):
            if v == s:
                return up

            nonlocal level, iter

            res = 0
            level_v = level[v]
            while iter[v] < len(self.G[v]):
                i = iter[v]
                iter[v] += 1
                e = self.G[v][i]
                if level_v <= level[e.to] or self.G[e.to][e.rev].cap == 0:
                    continue

                d = dfs(e.to, min(up - res, self.G[e.to][e.rev].cap))
                if d <= 0:
                    continue

                self.G[v][i].cap += d
                self.G[e.to][e.rev].cap -= d
                res += d
                if res == up:
                    return res

            level[v] = self.n
            return res

        flow = 0
        while flow < flow_limit:
            bfs()
            if level[t] == -1:
                break

            iter = [0] * self.n
            f = dfs(t, flow_limit - flow)
            if f == 0:
                break
            flow += f

        return flow

    def min_cut(self, s):
        visited = [False] * self.n
        que = deque()
        que.append(s)
        while que:
            p = que.popleft()
            visited[p] = True
            for e in self.G[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que.append(e.to)

        return visited


n, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = sum(B)
G = mf_graph(n + m + 2)
s = n + m
t = n + m + 1

for i in range(n):
    G.add_edge(s, i, A[i])
for i in range(m):
    G.add_edge(n + i, t, B[i])
    k, *C = map(int, input().split())
    for c in C:
        G.add_edge(c - 1, n + i, 1 << 60)

print(ans - G.flow(s, t))
0