結果

問題 No.2320 Game World for PvP
ユーザー rniyarniya
提出日時 2023-05-26 22:16:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 8,928 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 217,732 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 12:02:46
合計ジャッジ時間 4,206 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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 ProjectSelectionProblem_Impl {

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;
    }
    int add_vertex() {
        g.resize(_n + 1);
        return _n++;
    }

    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);
        atcoder::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);
        atcoder::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;
};

template <typename T> struct ProjectSelectionProblem {
    ProjectSelectionProblem(int n) : n(n + 2), s(n), t(n + 1), sum(T(0)), graph(n + 2) {}

    void x_false_loss(int x, T z) {
        assert(0 <= x and x < n);
        graph.add_edge(x, t, z);
    }

    void x_false_profit(int x, T z) {
        assert(0 <= x and x < n);
        sum += z;
        x_true_loss(x, z);
    }

    void x_true_loss(int x, T z) {
        assert(0 <= x and x < n);
        graph.add_edge(s, x, z);
    }

    void x_true_profit(int x, T z) {
        assert(0 <= x and x < n);
        sum += z;
        x_false_loss(x, z);
    }

    void x_false_y_true_loss(int x, int y, T z) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        graph.add_edge(x, y, z);
    }

    void x_true_y_false_loss(int x, int y, T z) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        graph.add_edge(y, x, z);
    }

    void x_false_y_false_profit(int x, int y, T z) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        sum += z;
        int w = graph.add_vertex();
        n++;
        x_true_loss(w, z);
        x_false_y_true_loss(w, x, inf);
        x_false_y_true_loss(w, y, inf);
    }

    void x_true_y_true_profit(int x, int y, T z) {
        assert(0 <= x and x < n);
        assert(0 <= y and y < n);
        sum += z;
        int w = graph.add_vertex();
        n++;
        x_false_loss(w, z);
        x_true_y_false_loss(w, x, inf);
        x_true_y_false_loss(w, y, inf);
    }

    T min_loss() { return graph.flow(s, t) - sum; }

    T max_profit() { return -min_loss(); }

private:
    int n, s, t;
    T sum;
    const T inf = std::numeric_limits<T>::max() / 2;
    mf_graph<T> graph;
};

}  // namespace ProjectSelectionProblem_Impl

using ProjectSelectionProblem_Impl::ProjectSelectionProblem;

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); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, S, T;
    cin >> N >> S >> T;
    vector<int> E(S), R(T);
    for (int& x : E) cin >> x, x--;
    for (int& x : R) cin >> x, x--;
    vector C(N, vector<int>(N));
    cin >> C;

    vector<int> idx(N, 0);
    for (int& x : E) idx[x] = -1;
    for (int& x : R) idx[x] = -2;
    vector<int> rest;
    for (int i = 0; i < N; i++) {
        if (idx[i] == 0) {
            idx[i] = rest.size();
            rest.emplace_back(i);
        }
    }
    int n = rest.size();
    ProjectSelectionProblem<ll> PSP(n);
    ll ans = 0;
    for (int i = 0; i < S; i++) {
        for (int j = i + 1; j < S; j++) {
            ans += C[E[i]][E[j]];
        }
    }
    for (int i = 0; i < T; i++) {
        for (int j = i + 1; j < T; j++) {
            ans += C[R[i]][R[j]];
        }
    }
    for (int i = 0; i < N; i++) {
        if (idx[i] < 0) continue;
        for (int j = 0; j < N; j++) {
            if (idx[j] == -1)
                PSP.x_false_profit(idx[i], C[i][j]);
            else if (idx[j] == -2)
                PSP.x_true_profit(idx[i], C[i][j]);
            else if (i < j) {
                PSP.x_false_y_false_profit(idx[i], idx[j], C[i][j]);
                PSP.x_true_y_true_profit(idx[i], idx[j], C[i][j]);
            }
        }
    }
    ans += PSP.max_profit();
    cout << ans << '\n';
    return 0;
}
0