結果

問題 No.2320 Game World for PvP
ユーザー ぷらぷら
提出日時 2023-07-25 02:52:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,936 bytes
コンパイル時間 2,563 ms
コンパイル使用メモリ 207,840 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 21:17:02
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

long long inf = 1001001001001001001;

template< typename flow_t >
struct Dinic {
    const flow_t INF;
    
    struct edge {
        int to;
        flow_t cap;
        int rev;
        bool isrev;
        int idx;
    };
    
    vector< vector< edge > > graph;
    vector< int > min_cost, iter;
    
    Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}
    
    void add_edge(int from, int to, flow_t cap, int idx = -1) {
        graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});
        graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});
    }
    
    bool bfs(int s, int t) {
        min_cost.assign(graph.size(), -1);
        queue< int > que;
        min_cost[s] = 0;
        que.push(s);
        while(!que.empty() && min_cost[t] == -1) {
            int p = que.front();
            que.pop();
            for(auto &e : graph[p]) {
                if(e.cap > 0 && min_cost[e.to] == -1) {
                    min_cost[e.to] = min_cost[p] + 1;
                    que.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }
    
    flow_t dfs(int idx, const int t, flow_t flow) {
        if(idx == t) return flow;
        for(int &i = iter[idx]; i < graph[idx].size(); i++) {
            edge &e = graph[idx][i];
            if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
                flow_t d = dfs(e.to, t, min(flow, e.cap));
                if(d > 0) {
                    e.cap -= d;
                    graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    
    flow_t max_flow(int s, int t) {
        flow_t flow = 0;
        while(bfs(s, t)) {
            iter.assign(graph.size(), 0);
            flow_t f = 0;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
        return flow;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,S,T;
    cin >> N >> S >> T;
    vector<int>f(N,-1);
    for(int i = 0; i < S; i++) {
        int E;
        cin >> E;
        E--;
        f[E] = 0;
    }
    for(int i = 0; i < T; i++) {
        int R;
        cin >> R;
        R--;
        f[R] = 1;
    }
    Dinic<long long> flow(N+2);
    int s = N,t = N+1;
    long long sum = 0;
    for(int i = 0; i < N; i++) {
        if(f[i] == -1) {
            flow.add_edge(s,i,0);
            flow.add_edge(i,t,0);
        }
        if(f[i] == 0) {
            flow.add_edge(s,i,0);
            flow.add_edge(i,t,inf);
        }
        if(f[i] == 1) {
            flow.add_edge(s,i,inf);
            flow.add_edge(i,t,0);
        }
        for(int j = 0; j < N; j++) {
            int C;
            cin >> C;
            sum += C;
            flow.add_edge(i,j,C);
        }
    }
    sum /= 2;
    cout << sum-flow.max_flow(s,t) << "\n";
}
0