結果

問題 No.2713 Just Solitaire
ユーザー 👑 Nafmo2Nafmo2
提出日時 2024-03-12 02:23:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,982 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 13,440 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-09-29 22:08:27
合計ジャッジ時間 2,973 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,264 KB
testcase_01 AC 34 ms
11,264 KB
testcase_02 AC 34 ms
11,264 KB
testcase_03 AC 36 ms
11,264 KB
testcase_04 AC 41 ms
11,904 KB
testcase_05 AC 34 ms
11,264 KB
testcase_06 AC 39 ms
12,032 KB
testcase_07 AC 36 ms
11,520 KB
testcase_08 AC 33 ms
11,264 KB
testcase_09 AC 34 ms
11,264 KB
testcase_10 AC 34 ms
11,264 KB
testcase_11 AC 36 ms
11,520 KB
testcase_12 AC 34 ms
11,264 KB
testcase_13 AC 40 ms
11,648 KB
testcase_14 AC 39 ms
11,648 KB
testcase_15 AC 32 ms
11,264 KB
testcase_16 AC 33 ms
11,264 KB
testcase_17 AC 39 ms
11,648 KB
testcase_18 AC 33 ms
11,264 KB
testcase_19 AC 41 ms
12,032 KB
testcase_20 AC 32 ms
11,264 KB
testcase_21 AC 33 ms
11,264 KB
testcase_22 AC 33 ms
11,264 KB
testcase_23 AC 38 ms
11,648 KB
testcase_24 AC 48 ms
12,672 KB
testcase_25 AC 49 ms
12,672 KB
testcase_26 AC 47 ms
12,544 KB
testcase_27 AC 48 ms
12,672 KB
testcase_28 AC 50 ms
12,672 KB
testcase_29 AC 48 ms
12,800 KB
testcase_30 AC 49 ms
12,672 KB
testcase_31 AC 47 ms
12,544 KB
testcase_32 AC 50 ms
12,672 KB
testcase_33 AC 48 ms
12,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#! /usr/bin/env python3
from collections import deque


class mf_graph:
    n = 1
    g = [[] for i in range(1)]
    pos = []

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

    def add_edge(self, From, To, cap):
        assert 0 <= From and From < self.n
        assert 0 <= To and To < self.n
        assert 0 <= cap
        m = len(self.pos)
        from_id = len(self.g[From])
        self.pos.append([From, from_id])
        to_id = len(self.g[To])
        if From == To:
            to_id += 1
        self.g[From].append([To, to_id, cap])
        self.g[To].append([From, from_id, 0])
        return m

    def get_edge(self, i):
        m = len(self.pos)
        assert 0 <= i and i < m
        _e = self.g[self.pos[i][0]][self.pos[i][1]]
        _re = self.g[_e[0]][_e[1]]
        return [self.pos[i][0], _e[0], _e[2] + _re[2], _re[2]]

    def edges(self):
        m = len(self.pos)
        result = []
        for i in range(m):
            a, b, c, d = self.get_edge(i)
            result.append({"from": a, "to": b, "cap": c, "flow": d})
        return result

    def change_edge(self, i, new_cap, new_flow):
        m = len(self.pos)
        assert 0 <= i and i < m
        assert 0 <= new_flow and new_flow <= new_cap
        _e = self.g[self.pos[i][0]][self.pos[i][1]]
        _re = self.g[_e[0]][_e[1]]
        _e[2] = new_cap - new_flow
        _re[2] = new_flow

    def flow(self, s, t, flow_limit=(1 << 63) - 1):
        assert 0 <= s and s < self.n
        assert 0 <= t and t < self.n
        assert s != t

        def bfs():
            level = [-1 for i in range(self.n)]
            level[s] = 0
            que = deque([])
            que.append(s)
            while que:
                v = que.popleft()
                for to, rev, cap in self.g[v]:
                    if cap == 0 or level[to] >= 0:
                        continue
                    level[to] = level[v] + 1
                    if to == t:
                        return level
                    que.append(to)
            return level

        def dfs(v, up):
            if v == s:
                return up
            res = 0
            level_v = level[v]
            for i in range(Iter[v], len(self.g[v])):
                Iter[v] = i
                to, rev, cap = self.g[v][i]
                if level_v <= level[to] or self.g[to][rev][2] == 0:
                    continue
                d = dfs(to, min(up - res, self.g[to][rev][2]))
                if d <= 0:
                    continue
                self.g[v][i][2] += d
                self.g[to][rev][2] -= d
                res += d
                if res == up:
                    return res
            level[v] = self.n
            return res

        flow = 0
        while flow < flow_limit:
            level = bfs()
            if level[t] == -1:
                break
            Iter = [0 for i in range(self.n)]
            f = dfs(t, flow_limit - flow)
            if not (f):
                break
            flow += f
        return flow

    def min_cut(self, s):
        visited = [False for i in range(self.n)]
        que = deque([])
        que.append(s)
        while len(que) > 0:
            p = que.popleft()
            visited[p] = True
            for to, rev, cap in self.g[p]:
                if cap and not (visited[to]):
                    visited[to] = True
                    que.append(to)
        return visited


ans = 0
INF = 10**15
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

s = N + M
t = N + M + 1
graph = mf_graph(N + M + 2)

for i in range(N):
    graph.add_edge(s, i, A[i])
for i in range(M):
    graph.add_edge(N + i, t, B[i])

for i in range(M):
    raw_C = list(map(int, input().split()))
    K = raw_C[0]
    C = raw_C[1:]
    for j in range(K):
        graph.add_edge(C[j] - 1, N + i, INF)

ans = sum(B) - graph.flow(s,t)
print(ans)
0