結果

問題 No.177 制作進行の宮森あおいです!
ユーザー kimiyukikimiyuki
提出日時 2016-01-16 02:19:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,945 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 81,260 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 23:54:36
合計ジャッジ時間 1,742 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <queue>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
using namespace std;
ll maximum_flow(int s, int t, vector<vector<ll> > const & g /* capacity, adjacency matrix */) { // edmonds karp, O(E^2V)
    int n = g.size();
    vector<vector<ll> > flow(n, vector<ll>(n));
    auto residue = [&](int i, int j) { return g[i][j] - flow[i][j]; };
    ll result = 0;
    while (true) {
        vector<int> prev(n, -1);
        vector<ll> f(n);
        // find the shortest augmenting path
        queue<int> q; // bfs
        q.push(s);
        while (not q.empty()) {
            int i = q.front(); q.pop();
            repeat (j,n) if (prev[j] == -1 and j != s and residue(i,j) > 0) {
                prev[j] = i;
                f[j] = residue(i,j);
                if (i != s) f[j] = min(f[j], f[i]);
                q.push(j);
            }
        }
        if (prev[t] == -1) break; // not found
        // backtrack
        for (int i = t; prev[i] != -1; i = prev[i]) {
            int j = prev[i];
            flow[j][i] += f[t];
            flow[i][j] -= f[t];
        }
        result += f[t];
    }
    return result;
}
int main() {
    // input
    int w, n; cin >> w >> n;
    vector<int> j(n); repeat (i,n) cin >> j[i];
    int m; cin >> m;
    vector<int> c(m); repeat (i,m) cin >> c[i];
    vector<set<int> > x(m);
    repeat (i,m) {
        int q; cin >> q;
        repeat (j,q) {
            int y; cin >> y;
            x[i].insert(y-1);
        }
    }
    // make a capacity graph
    const ll INF = 1000000007;
    vector<vector<ll> > g(n+m+2, vector<ll>(n+m+2));
    int s = n+m, t = n+m+1;
    repeat (i,n) g[s][i] = j[i];
    repeat (j,m) g[n+j][t] = c[j];
    repeat (j,m) repeat (i,n) if (not x[j].count(i)) g[i][n+j] = INF;
    // output
    cout << (maximum_flow(s,t,g) >= w ? "SHIROBAKO" : "BANSAKUTSUKITA") << endl;
    return 0;
}
0