結果

問題 No.177 制作進行の宮森あおいです!
ユーザー legosukelegosuke
提出日時 2020-03-02 02:00:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,930 bytes
コンパイル時間 2,459 ms
コンパイル使用メモリ 209,296 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 22:28:20
合計ジャッジ時間 3,279 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

template <typename T> class Dinic {
    struct edge {
        int to, rev;
        T cap;
        bool isrev;
        edge(int t, T c, int r, bool i) : to(t), cap(c), rev(r), isrev(i) {}
    };

    void bfs(int s, int t) {
        lev.assign(g.size(), -1);
        queue<int> que;
        lev[s] = 0;
        que.emplace(s);
        while (!que.empty() && lev[t] == -1) {
            int v = que.front();
            que.pop();
            for (edge& e : g[v]) {
                if (lev[e.to] == -1 && e.cap > 0) {
                    lev[e.to] = lev[v] + 1;
                    que.emplace(e.to);
                }
            }
        }
    }

    T dfs(int v, int t, T f) {
        if (v == t) return f;
        for (int& i = ite[v]; i < g[v].size(); ++i) {
            edge& e = g[v][i];
            if (e.cap > 0 && lev[v] < lev[e.to]) {
                T d = dfs(e.to, t, min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:

    const T INF;
    vector<int> lev, ite;
    vector<vector<edge>> g;

    Dinic(int n) : INF(numeric_limits<T>::max()), g(n) {}

    void add_edge(int f, int t, T c = numeric_limits<T>::max()) {
        g[f].emplace_back(t, c, g[t].size(), false);
        g[t].emplace_back(f, 0, g[f].size() - 1, true);
    }

    T max_flow(int s, int t) {
        T res = 0;
        while (1) {
            bfs(s, t);
            if (lev[t] < 0) break;
            ite.assign(g.size(), 0);
            T f = 0;
            while ((f = dfs(s, t, INF)) > 0) {
                res += f;
            }
        }
        return res;
    }

    void print() {
        for (int i = 0; i < g.size(); ++i) {
            for (auto& e : g[i]) {
                if (e.isrev) continue;
                auto& rev_e = g[e.to][e.rev];
                cout << i << " -> " << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
    }
};

int W, N, M;
int J[50], C[50];
int Q[50], ng[50][50];

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

    cin >> W >> N;
    REP (i, N) cin >> J[i];
    cin >> M;
    REP (i, M) cin >> C[i];
    Dinic<int> g(N + M + 2);
    REP (i, N) g.add_edge(N + M, i, J[i]);
    REP (i, M) g.add_edge(N + i, N + M + 1, C[i]);
    REP (i, M) {
        cin >> Q[i];
        REP (j, Q[i]) {
            int X;
            cin >> X;
            ng[X - 1][i] = 1;
        }
    }
    REP (i, N) REP (j, M) {
        if (ng[i][j]) g.add_edge(i, N + j, 0);
        else g.add_edge(i, N + j);
    }
    cout << (g.max_flow(N + M, N + M + 1) >= W ? "SHIROBAKO" : "BANSAKUTSUKITA") << endl;
}
0