結果

問題 No.2713 Just Solitaire
ユーザー MitI_7MitI_7
提出日時 2024-12-15 10:44:43
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 12,533 bytes
コンパイル時間 6,885 ms
コンパイル使用メモリ 192,660 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-15 10:44:54
合計ジャッジ時間 7,874 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 4 ms
6,816 KB
testcase_05 AC 3 ms
6,820 KB
testcase_06 AC 3 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 3 ms
6,820 KB
testcase_11 AC 3 ms
6,820 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 3 ms
6,820 KB
testcase_14 AC 3 ms
6,820 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 3 ms
6,816 KB
testcase_18 AC 2 ms
6,820 KB
testcase_19 AC 3 ms
6,820 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 2 ms
6,816 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 3 ms
6,820 KB
testcase_24 AC 5 ms
6,820 KB
testcase_25 AC 4 ms
6,816 KB
testcase_26 AC 4 ms
6,816 KB
testcase_27 AC 4 ms
6,820 KB
testcase_28 AC 5 ms
6,820 KB
testcase_29 AC 4 ms
6,816 KB
testcase_30 AC 4 ms
6,820 KB
testcase_31 AC 4 ms
6,816 KB
testcase_32 AC 5 ms
6,820 KB
testcase_33 AC 4 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define LEN(x) (long long)(x.size())
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define BIT_COUNT32(bit) (__builtin_popcount(bit))
#define BIT_COUNT64(bit) (__builtin_popcountll(bit))

template<typename T> using MinPriorityQueue = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template<typename T> using MaxPriorityQueue = std::priority_queue<T>;

// @formatter:off
typedef long long LL;
typedef __int128_t LLL;
template<typename T> std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts> auto make_v(size_t a, Ts... ts){ return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}    // C++14
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
void print() { std::cout << std::endl; }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { std::cout << head; if (sizeof...(tail) != 0) {std::cout << " ";} print(std::forward<Tail>(tail)...); }
template <class T> void print(std::vector<T> &v) {for (auto& a : v) { std::cout << a; if (&a != &v.back()) {std::cout << " ";} }std::cout << std::endl;}
template <class T> void print(std::pair<T, T> &p) { std::cout << p.first << " " << p.second << std::endl; }
void debug() { std::cerr << std::endl; }
template <class Head, class... Tail> void debug(Head&& head, Tail&&... tail) { std::cerr << head; if (sizeof...(tail) != 0) {std::cerr << " ";} debug(std::forward<Tail>(tail)...); }
template <class T> void debug(std::vector<T> &v) {for (auto& a : v) { std::cerr << a; if (&a != &v.back()) {std::cerr << " ";} }std::cerr << std::endl;}
template <class T> void debug(std::pair<T, T> &p) { std::cerr << p.first << " " << p.second << std::endl; }
inline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline T euclidean_distance2(T y1, T x1, T y2, T x2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); }
template<class T> inline T manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }
template<typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }
bool is_bit_on(const unsigned long long bit, const unsigned int i) { return (bit >> i) & 1u; }
unsigned long long get_bit_set(const unsigned long long bit, const unsigned int i, const unsigned int b) { assert(b == 0 or b == 1); if (b == 0) { return bit & ~(1ull << i); } else {return bit | (1ull << i);}}

// 初項s交差d長さnの数列の和
long long sum_of_arithmetic_progression(long long s, long long d, long long n) {
    return n * (2 * s + (n - 1) * d) / 2;
}

// 三角数
long long triangular_number(long long n) {
    return n * (n + 1) / 2;
}

// sqrt(x)の整数解を求める
// 整数解がなければ-1
long long sqrt_integer(const long long x) {
    if (x < 0) {
        return -1;
    }
    auto a = (long long)sqrt(x);
    if (a * a == x) {
        return a;
    }
    if((a - 1) * (a - 1) == x) {
        return a - 1;
    }
    if((a + 1) * (a + 1) == x) {
        return a + 1;
    }

    return -1;
}

// xが2の階乗かどうか判定
bool is_power_of_two(long long x) {
    return !(x & (x - 1));
}

// O(log max(a, b))
long long gcd(long long a, long long b) {
    if (b == 0) { return a; }
    return gcd(b, a % b);
}

long long lcm(long long a, long long b) {
    long long g = gcd(a, b);
    return a / g * b;
}

const int INF = 1u << 30u;  // 1,073,741,824
const long long LINF = 1ull << 60u;
const double EPS = 1e-9;
const long double PI = acos(-1.0);
// 2次元配列上での移動.右,下,左,上,右上,右下,左下,左上
const std::vector<int> dy8 = {0, 1, 0, -1, -1, 1, 1, -1}, dx8 = {1, 0, -1, 0, 1, 1, -1, -1};
// @formatter:on

// 最大流問題を解く O(|E||V|^2)
class Dinic {
public:
    struct Edge {
        const unsigned int to; // 行き先のノードid
        long long flow; // 流量
        const long long cap; // 容量
        const unsigned int rev; // 逆辺のノードid
        const bool is_rev; // 逆辺かどうか
        Edge(int to, long long flow, long long cap, int rev, bool is_rev)
            : to(to), flow(flow), cap(cap), rev(rev), is_rev(is_rev) {
            assert(this->cap >= 0);
        }
    };

    std::vector<std::vector<Edge>> graph; // グラフの隣接リスト表現
    std::vector<int> level; // sからの距離
    std::vector<unsigned int> iter; // どこまで調べ終わったか

    Dinic(unsigned int num_of_node) {
        assert(num_of_node > 0);
        this->graph.resize(num_of_node);
        this->level.resize(num_of_node);
        this->iter.resize(num_of_node);
    }

    // fromからtoへ向かう容量capの辺をグラフに追加する
    void add_edge(unsigned int from, unsigned int to, long long cap) {
        this->graph[from].emplace_back(
            Edge(to, 0, cap, (unsigned int)graph[to].size(), false));
        this->graph[to].emplace_back(
            Edge(from, cap, cap, (unsigned int)graph[from].size() - 1, true));
    }

    // sからtへの最大流を求める
    long long max_flow(unsigned int s, unsigned int t) {
        long long flow = 0;
        while (true) {
            this->bfs(s);
            if (this->level[t] < 0) {
                return flow;
            }
            fill(this->iter.begin(), this->iter.end(), 0);
            long long f;

            while ((f = dfs(s, t, 10000000000ll)) > 0) {
                flow += f;
            }
        }
    }

private:
    // sからの最短距離をBFSで計算する
    void bfs(unsigned int s) {
        fill(this->level.begin(), this->level.end(), -1);
        std::queue<unsigned int> que;
        this->level[s] = 0;
        que.push(s);
        while (not que.empty()) {
            unsigned int v = que.front();
            que.pop();
            for (int i = 0; i < (int)this->graph[v].size(); ++i) {
                Edge &e = this->graph[v][i];
                if ((e.cap - e.flow) > 0 and level[e.to] < 0) {
                    this->level[e.to] = this->level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }

    // 増加パスをDFSで探す
    long long dfs(unsigned int v, unsigned int t, long long f) {
        if (v == t) {
            return f;
        }
        for (unsigned int &i = this->iter[v]; i < this->graph[v].size(); ++i) {
            Edge &e = this->graph[v][i];
            if ((e.cap - e.flow) > 0 and this->level[v] < this->level[e.to]) {
                long long d = dfs(e.to, t, std::min(f, e.cap - e.flow));
                if (d > 0) {
                    e.flow += d;
                    this->graph[e.to][e.rev].flow -= d;
                    return d;
                }
            }
        }

        return 0;
    }
};

template <class COST>
class SubmodularQUBOMinimizer {
    int num_variables;
    std::vector<std::array<COST, 2>> unary_costs;
    std::map<std::pair<int, int>, std::array<COST, 4>> pair_wise_costs;

public:
    SubmodularQUBOMinimizer(): num_variables(0) {
    }

    int add_variable() {
        this->add_variables(1);
        return this->num_variables;
    }

    std::vector<int> add_variables(const int num) {
        std::vector<int> nodes(num);
        std::iota(nodes.begin(), nodes.end(), this->num_variables);
        this->num_variables += num;
        this->unary_costs.resize(this->num_variables);
        return nodes;
    }

    void add_unary_0_cost(int i, COST cost) {
        assert(0 <= i and i < this->num_variables);
        this->unary_costs[i][0b0] += cost;
    }

    void add_unary_1_cost(int i, COST cost) {
        assert(0 <= i and i < this->num_variables);
        this->unary_costs[i][0b1] += cost;
    }

    void add_pairwise_00_cost(int i, int j, COST cost) {
        assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables);
        if (i > j) {
            std::swap(i, j);
        }
        this->pair_wise_costs[{i, j}][0b00] += cost;
    }

    void add_pairwise_01_cost(int i, int j, COST cost) {
        assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables);
        this->pair_wise_costs[{i, j}][0b01] += cost;
    }

    void add_pairwise_10_cost(int i, int j, COST cost) {
        assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables);
        this->pair_wise_costs[{i, j}][0b10] += cost;
    }

    void add_pairwise_11_cost(int i, int j, COST cost) {
        assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables);
        if (i > j) {
            std::swap(i, j);
        }
        this->pair_wise_costs[{i, j}][0b11] += cost;
    }

    COST solve() {
        const auto offset = this->re_parameterization();

        Dinic dinic(2 + num_variables);
        const int s = num_variables;
        const int t = s + 1;

        for (int i = 0; i < num_variables; ++i) {
            if (this->unary_costs[i][0b0] > 0) {
                dinic.add_edge(s, i, this->unary_costs[i][0]);
            }
            if (this->unary_costs[i][0b1] > 0) {
                dinic.add_edge(i, t, this->unary_costs[i][1]);
            }
        }

        for (auto &[key, cost] : this->pair_wise_costs) {
            auto [i, j] = key;
            if (cost[0b10] > 0) {
                dinic.add_edge(i, j, cost[0b10]);
            }
            if (cost[0b01] > 0) {
                dinic.add_edge(j, i, cost[0b01]);
            }
        }

        return dinic.max_flow(s, t) + offset;
    }

private:
    COST re_parameterization() {
        COST offset = 0;

        for (auto &[key, cost] : this->pair_wise_costs) {
            const auto [i, j] = key;
            {
                const auto delta = std::min(cost[0b00], std::min(cost[0b01], std::min(cost[0b10], cost[0b11])));
                cost[0b00] -= delta;
                cost[0b01] -= delta;
                cost[0b10] -= delta;
                cost[0b11] -= delta;
                offset += delta;
            }

            for (int b = 0; b <= 1; ++b) {
                const auto delta = std::min(cost[0b00 | b], cost[0b10 | b]);
                cost[0b00 | b] -= delta;
                cost[0b10 | b] -= delta;
                this->unary_costs[j][b] -= delta;
            }

            for (int a = 0; a <= 1; ++a) {
                const auto delta = std::min(cost[a << 1 | 0b00], cost[a << 1 | 0b01]);
                cost[a << 1 | 0b00] -= delta;
                cost[a << 1 | 0b01] -= delta;
                this->unary_costs[j][a] -= delta;
            }
        }

        for (int i = 0; i < this->num_variables; ++i) {
            const auto delta = std::min(this->unary_costs[i][0], this->unary_costs[i][1]);
            this->unary_costs[i][0b0] -= delta;
            this->unary_costs[i][0b1] -= delta;
            offset += delta;
        }

        return offset;
    }
};

using namespace std;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;

    SubmodularQUBOMinimizer<long long> solver;
    auto xs = solver.add_variables(N);
    auto ys = solver.add_variables(M);

    FOR(i, 0, N) {
        LL A;
        cin >> A;
        solver.add_unary_1_cost(xs[i], A);
    }

    FOR(j, 0, M) {
        LL B;
        cin >> B;
        solver.add_unary_1_cost(ys[j], -B);
    }

    FOR(j, 0, M) {
        int K;
        cin >> K;
        FOR(_, 0, K) {
            int C;
            cin >> C;
            C--;
            solver.add_pairwise_01_cost(xs[C], ys[j], INF);
        }
    }
    print(-solver.solve());

    return 0;
}
0