結果

問題 No.1984 [Cherry 4th Tune *] Dilemma
ユーザー sotanishysotanishy
提出日時 2022-06-17 23:22:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 3,293 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-11-08 14:05:15
合計ジャッジ時間 10,398 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,400 KB
testcase_01 AC 45 ms
54,400 KB
testcase_02 AC 50 ms
54,528 KB
testcase_03 AC 49 ms
54,528 KB
testcase_04 AC 45 ms
54,272 KB
testcase_05 AC 45 ms
54,656 KB
testcase_06 AC 45 ms
54,272 KB
testcase_07 AC 49 ms
54,528 KB
testcase_08 AC 44 ms
54,656 KB
testcase_09 AC 49 ms
54,144 KB
testcase_10 AC 49 ms
54,144 KB
testcase_11 AC 46 ms
54,784 KB
testcase_12 AC 45 ms
54,784 KB
testcase_13 AC 50 ms
55,808 KB
testcase_14 AC 97 ms
77,312 KB
testcase_15 AC 72 ms
66,176 KB
testcase_16 AC 79 ms
69,632 KB
testcase_17 AC 84 ms
72,320 KB
testcase_18 AC 71 ms
66,816 KB
testcase_19 AC 75 ms
66,560 KB
testcase_20 AC 50 ms
54,528 KB
testcase_21 AC 72 ms
67,328 KB
testcase_22 AC 85 ms
73,856 KB
testcase_23 AC 95 ms
77,568 KB
testcase_24 AC 98 ms
77,568 KB
testcase_25 AC 100 ms
77,696 KB
testcase_26 AC 95 ms
77,696 KB
testcase_27 AC 108 ms
77,580 KB
testcase_28 AC 97 ms
77,056 KB
testcase_29 AC 99 ms
77,056 KB
testcase_30 AC 98 ms
77,952 KB
testcase_31 AC 89 ms
73,728 KB
testcase_32 AC 110 ms
77,568 KB
testcase_33 AC 61 ms
62,720 KB
testcase_34 AC 67 ms
65,408 KB
testcase_35 AC 61 ms
62,720 KB
testcase_36 AC 59 ms
61,824 KB
testcase_37 AC 61 ms
62,336 KB
testcase_38 AC 59 ms
61,696 KB
testcase_39 AC 75 ms
67,968 KB
testcase_40 AC 56 ms
61,184 KB
testcase_41 AC 60 ms
61,568 KB
testcase_42 AC 58 ms
62,592 KB
testcase_43 AC 60 ms
63,232 KB
testcase_44 AC 55 ms
61,824 KB
testcase_45 AC 65 ms
65,152 KB
testcase_46 AC 70 ms
67,840 KB
testcase_47 AC 70 ms
65,664 KB
testcase_48 AC 67 ms
65,920 KB
testcase_49 AC 66 ms
65,664 KB
testcase_50 AC 61 ms
64,256 KB
testcase_51 AC 65 ms
66,176 KB
testcase_52 AC 65 ms
66,432 KB
testcase_53 AC 61 ms
64,896 KB
testcase_54 AC 64 ms
66,048 KB
testcase_55 AC 75 ms
67,328 KB
testcase_56 AC 74 ms
66,304 KB
testcase_57 AC 71 ms
66,048 KB
testcase_58 AC 56 ms
62,848 KB
testcase_59 AC 76 ms
70,656 KB
testcase_60 AC 67 ms
67,328 KB
testcase_61 AC 67 ms
66,816 KB
testcase_62 AC 71 ms
68,608 KB
testcase_63 AC 95 ms
77,824 KB
testcase_64 AC 76 ms
71,424 KB
testcase_65 AC 78 ms
71,936 KB
testcase_66 AC 47 ms
55,040 KB
testcase_67 AC 58 ms
60,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class Dinic:
    class Edge:
        def __init__(self, v, cap, rev):
            self.v = v
            self.cap = cap
            self.rev = rev

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

    def add_edge(self, u, v, cap):
        self.G[u].append(self.Edge(v, cap, len(self.G[v])))
        self.G[v].append(self.Edge(u, 0, len(self.G[u])-1))

    # calculate the shortest distance from s
    def bfs(self, s):
        from collections import deque

        self.level = [-1] * self.V
        self.level[s] = 0
        queue = deque()
        queue.append(s)
        while queue:
            u = queue.popleft()
            for e in self.G[u]:
                if e.cap > 0 and self.level[e.v] == -1:
                    self.level[e.v] = self.level[u] + 1
                    queue.append(e.v)

    # find a path
    def dfs(self, u, t, f):
        if u == t:
            return f
        i = self.iter[u]
        while i < len(self.G[u]):
            e = self.G[u][i]
            if e.cap > 0 and self.level[u] < self.level[e.v]:
                d = self.dfs(e.v, t, min(f, e.cap))
                if d > 0:
                    e.cap -= d
                    self.G[e.v][e.rev].cap += d
                    return d
            i += 1
            self.iter[u] += 1
        return 0

    # find the max flow from s to v
    def max_flow(self, s, t):
        flow = 0
        INF = 10**18
        while True:
            self.bfs(s)
            if self.level[t] == -1:
                return flow
            self.iter = [0] * self.V
            while True:
                f = self.dfs(s, t, INF)
                if f == 0:
                    break
                flow += f

    def min_cut(self, s):
        visited = [False] * self.V
        st = [s]
        visited[s] = True
        while st:
            u = st.pop()
            for e in self.G[u]:
                if e.cap > 0 and not visited[e.v]:
                    visited[e.v] = True
                    st.append(e.v)
        return visited



N, M, K, P = map(int, input().split())
E = list(map(int, input().split()))
F = list(map(int, input().split()))
V = list(map(int, input().split()))
flow = Dinic(N+M+K+2)
s = N+M+K
t = s+1
ans = 0
for i in range(N):
    flow.add_edge(s, i, E[i])
    flow.add_edge(i, t, 0)
    ans += E[i]
for i in range(M):
    flow.add_edge(s, N+i, 0)
    flow.add_edge(N+i, t, F[i])
    ans += F[i]
for i in range(K):
    flow.add_edge(s, N+M+i, 0)
    flow.add_edge(N+M+i, t, V[i])
for i in range(N):
    _, *A = list(map(int, input().split()))
    for a in A:
        a -= 1
        flow.add_edge(i, N+M+a, 10**18)
for i in range(P):
    I, J = map(int, input().split())
    I -= 1
    J -= 1
    flow.add_edge(I, N+J, 10**18)
ans -= flow.max_flow(s, t)
use = flow.min_cut(s)
goals = []
actions = []
preps = []
for i in range(N+M+K):
    if i < N:
        if use[i]:
            goals.append(i+1)
    elif i < N+M:
        if not use[i]:
            actions.append(i-N+1)
    else:
        if use[i]:
            preps.append(i-N-M+1)
print(ans)
print(len(goals) + len(actions) + len(preps))
for i in preps:
    print("Preparation", i)
for i in goals:
    print("Goal", i)
for i in actions:
    print("Action", i)
0