結果

問題 No.2713 Just Solitaire
ユーザー GOTKAKOGOTKAKO
提出日時 2024-03-31 16:56:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 2,758 ms
コンパイル使用メモリ 221,668 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-31 16:56:09
合計ジャッジ時間 3,926 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 3 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 3 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 3 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 3 ms
6,676 KB
testcase_24 AC 4 ms
6,676 KB
testcase_25 AC 4 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 4 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 4 ms
6,676 KB
testcase_31 AC 3 ms
6,676 KB
testcase_32 AC 4 ms
6,676 KB
testcase_33 AC 4 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long inf = 2e18;
struct edge{ int to; long long cap; int rev; bool isrev;};
class Dinic{
public:
    int siz = 0;
    vector<vector<edge>> Graph;
    vector<int> searched,dist;
    vector<bool> visited;
 
    void make(int N){
        siz = N;
        Graph.resize(N); visited.resize(N);
        dist.resize(N,-1);
    }
 
    void addedge(int u, int v, long long c){
        int gv = Graph.at(v).size(), gu = Graph.at(u).size();
        Graph.at(u).push_back({v,c,gv,false});
        Graph.at(v).push_back({u,0,gu,true});
    }
 
    void bfs(int s, int t){
        queue<int> Q; Q.push(s);
        dist.at(s) = 0;
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            for(auto &[to,c,r,ign] : Graph.at(pos)){
                if(c > 0 && dist.at(to) == -1){
                    dist.at(to) = dist.at(pos)+1;
                    if(to != t) Q.push(to);
                }
            }
        }
    }
 
    long long dfs(int pos, int start, long long f){
        if(pos == start) return f;
        visited.at(pos) = true;
 
        long long flow = 0;
        for(auto &[t,c,r,ign] : Graph.at(pos)){
            long long &revc = Graph.at(t).at(r).cap;
            if(visited.at(t) || revc == 0 || f-flow == 0 || dist.at(pos) <= dist.at(t)) continue;
            long long now = dfs(t,start,min(revc,f-flow));
            c += now; revc -= now;
 
            flow += now;
        }
        return flow;
    }
 
    long long maxflow(int s, int t){
        long long ret = 0;
        while(true){
            visited.assign(siz,false);
            dist.assign(siz,-1);
            bfs(s,t);
            if(dist.at(t) == -1) break;
            ret += dfs(t,s,inf);
        }
        return ret;
    }
 
    vector<pair<int,int>> mincost(int s){
        //maxflowの後の復元
        vector<int> distS(siz,-1);
        distS.at(s) = 1;
        queue<int> Q; Q.push(s);
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            for(auto &[to,c,r,ign] : Graph.at(pos)){
                if(c == 0 || distS.at(to) != -1) continue;
                distS.at(to) = 1; Q.push(to);
            }
        }
 
        vector<pair<int,int>> ret;
        for(int i=0; i<siz; i++){
            for(auto &[to,c,r,isr] : Graph.at(i)){
                if(to < i || isr) continue;
                if(distS.at(i) != distS.at(to)) ret.push_back({i,to});
            }
        }
        return ret;
    }
};

int main() {
    int N,M; cin >> N >> M;
    Dinic Z; Z.make(N+M+2);
    int s = N+M,t = N+M+1;

    long long answer = 0;
    for(int i=0; i<N; i++){
        int a; cin >> a;
        Z.addedge(s,i,a);
    }
    for(int i=0; i<M; i++){
        int b; cin >> b;
        answer += b;
        Z.addedge(N+i,t,b);
    }
    for(int i=0; i<M; i++){
        int K; cin >> K;
        for(int k=0; k<K; k++){
            int c; cin >> c; c--;
            Z.addedge(c,N+i,(long long)1e18);

        }
    }
    cout << answer-Z.maxflow(s,t) << endl;
}
0