結果

問題 No.1984 [Cherry 4th Tune *] Dilemma
ユーザー rniyarniya
提出日時 2024-03-31 19:02:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 11,579 bytes
コンパイル時間 3,420 ms
コンパイル使用メモリ 232,272 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-30 21:17:42
合計ジャッジ時間 9,388 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/1984"

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    explicit mf_graph(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

}  // namespace atcoder

template <typename T> struct ProjectSelectionProblem {
    ProjectSelectionProblem() = default;

    explicit ProjectSelectionProblem(int n) : n(n), costs(n, std::vector<T>(2, 0)) {}

    void add_cost_0(int x, T cost) {
        assert(0 <= x and x < n);
        assert(cost >= 0);
        add_cost(x, {cost, 0});
    }

    void add_cost_1(int x, T cost) {
        assert(0 <= x and x < n);
        assert(cost >= 0);
        add_cost(x, {0, cost});
    }

    void add_profit_0(int x, T profit) {
        assert(0 <= x and x < n);
        assert(profit >= 0);
        add_cost(x, {-profit, 0});
    }

    void add_profit_1(int x, T profit) {
        assert(0 <= x and x < n);
        assert(profit >= 0);
        add_cost(x, {0, -profit});
    }

    void add_cost_01(int x, int y, T cost) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        assert(cost >= 0);
        add_edge(x, y, cost);
    }

    void add_cost_10(int x, int y, T cost) { add_cost_01(y, x, cost); }

    void add_profit_00(int x, int y, T profit) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        assert(profit >= 0);
        add_cost(x, y, {-profit, 0, 0, 0});
    }

    void add_profit_11(int x, int y, T profit) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        assert(profit >= 0);
        add_cost(x, y, {0, 0, 0, -profit});
    }

    // void add_cost_for_3(int x, int y, int z, std::array<std::array<std::array<T, 2>, 2>, 2> cost) {
    //     add_cost(x, y, z, cost);
    // }

    void add_profit_all_0(const std::vector<int>& v, T profit) {
        assert(profit >= 0);
        if (v.size() == 0)
            base_cost -= profit;
        else if (v.size() == 1)
            add_profit_0(v[0], profit);
        else if (v.size() == 2)
            add_profit_00(v[0], v[1], profit);
        else {
            base_cost -= profit;
            int nxt = n + aux++;
            add_edge(source, nxt, profit);
            for (const int& x : v) add_edge(nxt, x, profit);
        }
    }

    void add_profit_all_1(const std::vector<int>& v, T profit) {
        assert(profit >= 0);
        if (v.size() == 0)
            base_cost -= profit;
        else if (v.size() == 1)
            add_profit_1(v[0], profit);
        else if (v.size() == 2)
            add_profit_11(v[0], v[1], profit);
        else {
            base_cost -= profit;
            int nxt = n + aux++;
            add_edge(nxt, sink, profit);
            for (const int& x : v) add_edge(x, nxt, profit);
        }
    }

    std::pair<T, std::vector<bool>> min_cost() {
        for (int i = 0; i < n; i++) {
            auto& tmp = costs[i];
            if (tmp[0] <= tmp[1]) {
                base_cost += tmp[0];
                add_edge(source, i, tmp[1] - tmp[0]);
            } else {
                base_cost += tmp[1];
                add_edge(i, sink, tmp[0] - tmp[1]);
            }
        }
        atcoder::mf_graph<T> g(n + aux + 2);
        int s = n + aux, t = s + 1;
        for (auto [u, v, w] : es) {
            u = (u == source ? s : u == sink ? t : u);
            v = (v == source ? s : v == sink ? t : v);
            g.add_edge(u, v, w);
        }
        auto sum = base_cost + g.flow(s, t);
        auto x = g.min_cut(s);
        x.resize(n);
        for (int i = 0; i < n; i++) x[i] = not x[i];
        return {sum, x};
    }

    std::pair<T, std::vector<bool>> max_profit() {
        auto res = min_cost();
        res.first *= -1;
        return res;
    }

  private:
    int n, aux = 0, source = -1, sink = -2;
    T base_cost = 0;
    std::vector<std::vector<T>> costs;
    std::vector<std::tuple<int, int, T>> es;

    void add_edge(int x, int y, T cost) {
        assert(x == source or x == sink or (0 <= x and x < n + aux));
        assert(y == source or y == sink or (0 <= y and y < n + aux));
        if (cost == 0) return;
        es.emplace_back(x, y, cost);
    }

    void add_cost(int x, std::array<T, 2> cost) {
        for (int i = 0; i < 2; i++) costs[x][i] += cost[i];
    }

    void add_cost(int x, int y, std::array<std::array<T, 2>, 2> cost) {
        assert(cost[0][1] + cost[1][0] >= cost[0][0] + cost[1][1]);
        base_cost += cost[0][0];
        add_cost(x, {0, cost[1][0] - cost[0][0]});
        add_cost(y, {0, cost[1][1] - cost[1][0]});
        add_cost_01(x, y, (cost[0][1] - cost[0][0]) - (cost[1][1] - cost[1][0]));
    }

    // void add_cost(int x, int y, std::array<std::array<std::array<T, 2>, 2>, 2> cost) {}
};

const long long inf = 1LL << 40;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M, K, P;
    std::cin >> N >> M >> K >> P;
    std::vector<int> E(N), F(M), V(K);
    for (int& val : E) cin >> val;
    for (int& val : F) cin >> val;
    for (int& val : V) cin >> val;
    ProjectSelectionProblem<long long> PSP(N + M + K);
    for (int i = 0; i < N; i++) {
        int L;
        cin >> L;
        for (; L--;) {
            int A;
            cin >> A;
            A--;
            PSP.add_cost_10(i, N + M + A, inf);
        }
        PSP.add_profit_1(i, E[i]);
    }
    for (int i = 0; i < M; i++) PSP.add_profit_0(N + i, F[i]);
    for (int i = 0; i < K; i++) PSP.add_cost_1(N + M + i, V[i]);
    for (; P--;) {
        int I, J;
        cin >> I >> J;
        I--, J--;
        PSP.add_cost_10(I, N + J, inf);
    }

    auto [C, res] = PSP.max_profit();
    std::cout << C << '\n';
    std::vector<int> motions;
    for (int i = N + M; i < N + M + K; i++) {
        if (res[i]) {
            std::cout << "Preparation " << i - (N + M) + 1 << '\n';
        }
    }
    for (int i = 0; i < N; i++) {
        if (res[i]) {
            std::cout << "Goal " << i + 1 << '\n';
        }
    }
    for (int i = N; i < N + M; i++) {
        if (not res[i]) {
            std::cout << "Action " << i - N + 1 << '\n';
        }
    }
    return 0;
}
0