結果

問題 No.2713 Just Solitaire
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-04-03 13:47:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,890 bytes
コンパイル時間 2,935 ms
コンパイル使用メモリ 223,524 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 13:47:49
合計ジャッジ時間 4,416 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct edge{
    int to; ll cap; int rev;
};

//Dinic(0-indexed)
struct MaxFlow{

    private:
        static const ll INF=9e18;
        vector<int> dist, iter;
        vector<pair<int, int>> idx;

        void bfs(int s){
            int from;
            fill(dist.begin(), dist.end(), -1);
            queue<int> que;
            
            que.push(s);
            dist[s] = 0;
            while(!que.empty()){
                from = que.front(); que.pop();
                for (auto &e : E[from]){
                    if (e.cap > 0 && dist[e.to] == -1){
                        dist[e.to] = dist[from] + 1;
                        que.push(e.to);
                    }
                }
            }
        }

        ll dfs(int s, int t, ll flow){
            if (s == t) return flow;

            for (int &i=iter[s]; i<E[s].size(); i++){
                edge &e = E[s][i];
                if (e.cap <= 0 || dist[s] >= dist[e.to]) continue;
                ll d = dfs(e.to, t, min(flow, e.cap));
                if (d > 0){
                    e.cap -= d;
                    E[e.to][e.rev].cap += d;
                    return d;
                }
            }
            return 0;
        }

    public:
        int N, M=0;
        vector<vector<edge>> E;

        MaxFlow(int n) : N(n) {
            E.resize(N);
            dist.resize(N);
            iter.resize(N);
        }

        void add_edge(int from, int to, ll cap){
            M++;
            idx.push_back({from, (int)E[from].size()});
            E[from].push_back({to, cap, (int)E[to].size()});
            E[to].push_back({from, 0, (int)E[from].size()-1});
        }

        ll solve(int s, int t, ll limit=INF){
            ll res = 0, flow;
            while(1){
                bfs(s);
                if (dist[t] == -1) break;

                for (int i=0; i<N; i++) iter[i] = 0;
                while((flow = dfs(s, t, INF)) > 0){
                    res += flow;
                    if (res >= limit) return limit;
                }
            }
            return res;
        }
        
        //i番目の辺の(from, to, flow)を求める。
        tuple<int, int, ll> get_flow(int i){
            assert (i < M);
            edge &e = E[idx[i].first][idx[i].second];
            return {idx[i].first, e.to, E[e.to][e.rev].cap};
        }

        //最小カット(s->t)を求める
        vector<tuple<int, int, ll>> get_cut(int s){
            vector<bool> vst(N);
            vector<tuple<int, int, ll>> res;
            auto dfs=[&](auto self, int from)->void{
                vst[from] = 1;
                for (auto &e : E[from]){
                    if (vst[e.to]) continue;
                    if (e.cap) self(self, e.to);
                }
            };
            dfs(dfs, s);
            for (int i=0; i<M; i++){
                edge &e = E[idx[i].first][idx[i].second];
                if (vst[idx[i].first] && !vst[e.to]) res.push_back({idx[i].first, e.to, E[e.to][e.rev].cap});
            }
            return res;
        }
};

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

    //C_1,..,C_K->DにコストBの辺
    //C_1,..,C_Kのうち、1つでも使わない側に行けばBの損失

    ll N, M, s=0, t, K, C, inf=1e18, ans=0;
    cin >> N >> M;
    MaxFlow mf(N+M+2);
    t = N+M+1;
    vector<ll> A(N+1), B(M+1);
    for (int i=1; i<=N; i++) cin >> A[i];
    for (int i=1; i<=M; i++){
        cin >> B[i];
        ans += B[i];
    }
    for (int i=1; i<=N; i++) mf.add_edge(i, t, A[i]);
    
    for (int i=1; i<=M; i++) mf.add_edge(s, i+N, B[i]);

    for (int i=1; i<=M; i++){
        cin >> K;
        for (int j=0; j<K; j++){
            cin >> C;
            mf.add_edge(i+N, C, inf);
        }
    }

    cout << ans-mf.solve(s, t) << endl;

    return 0;
}
0