結果

問題 No.904 サメトロ
ユーザー nawawannawawan
提出日時 2021-04-02 00:42:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,211 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 85,060 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 07:35:27
合計ジャッジ時間 2,489 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
template<typename T>
struct Dinic{
    struct edge{
        int to;
        T cap;
        int rev;
    };
    int V;
    T INF;
    vector<int> level, used;
    vector<vector<edge>> G;
    Dinic(int N): INF(numeric_limits<T>::max()), V(N), used(N), level(N), G(N){}
    void add(int u, int v, T c){
        edge e1 = {v, c, (int)G[v].size()};
        G[u].push_back(e1);
        edge e2 = {u, 0, (int)G[u].size() - 1};
        G[v].push_back(e2);
    }
    void bfs(int s){
        level.assign(V, -1);
        queue<int> q;
        q.push(s);
        level[s] = 0;
        while(!q.empty()){
            int v = q.front();
            q.pop();
            for(edge e: G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        }
    }
    T dfs(int s, int t, T f){
        if(s == t) return f;
        for(int &i = used[s]; i < (int)G[s].size(); i++){
            edge &e = G[s][i];
            if(e.cap > 0 && level[e.to] > level[s]){
                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;
    }
    T solve(int s, int t){
        T flow = 0;
        while(1){
            bfs(s);
            if(level[t] < 0) return flow;
            used.assign(V, 0);
            long long f;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
    }
};
int main(){
    int N;
    cin >> N;
    Dinic<int> D(N * 2 + 2);
    D.add(N * 2, 0, D.INF);
    D.add(N, N * 2 + 1, D.INF);
    int sum = 0;
    for(int i = 0; i < N - 1; i++){
        int a, b;
        cin >> a >> b;
        sum += a;
        D.add(N * 2, i + 1, a);
        D.add(N + i + 1, N * 2 + 1, b);
    }
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(i != j){
                D.add(i, N + j, D.INF);
            }
        }
    }
    int t = D.solve(2 * N, 2 * N + 1);
    cout << t - sum + 1 << endl;
}
0