結果

問題 No.654 Air E869120
ユーザー umimelumimel
提出日時 2022-10-11 23:39:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 3,374 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 195,932 KB
実行使用メモリ 5,364 KB
最終ジャッジ日時 2023-09-08 01:16:33
合計ジャッジ時間 4,662 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 35 ms
5,312 KB
testcase_11 AC 14 ms
4,600 KB
testcase_12 AC 20 ms
4,808 KB
testcase_13 AC 26 ms
4,732 KB
testcase_14 AC 9 ms
4,408 KB
testcase_15 AC 14 ms
4,752 KB
testcase_16 AC 9 ms
5,364 KB
testcase_17 AC 13 ms
5,328 KB
testcase_18 AC 8 ms
5,316 KB
testcase_19 AC 10 ms
5,228 KB
testcase_20 AC 5 ms
4,612 KB
testcase_21 AC 6 ms
4,852 KB
testcase_22 AC 5 ms
4,820 KB
testcase_23 AC 6 ms
4,752 KB
testcase_24 AC 5 ms
4,656 KB
testcase_25 AC 5 ms
4,596 KB
testcase_26 AC 6 ms
4,808 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 4 ms
4,376 KB
testcase_34 AC 4 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct Edge{
    ll to;
    ll cap;
    ll rev;
};

struct Ford_Fulkerson{
    vector<vector<Edge>> G;
    vector<bool> used;
    ll N;

    Ford_Fulkerson(vector<vector<pll>> G_){
        N = (ll)G_.size();
        G.resize(N);
        used.resize(N, false);

        rep(from, N){
            for(pll p : G_[from]){
                ll to = p.fi;
                ll cap = p.se;
                G[from].pb((Edge){to, cap, (ll)G[to].size()});
                G[to].pb((Edge){from, 0, (ll)(G[from].size()-1)});
            }
        }
    }

    ll dfs(ll v, ll t, ll f, vector<vector<Edge>> &H){
        if(v == t) return f;
        used[v] = true;
        for(ll i=0; i<(ll)H[v].size(); i++){
            Edge &e = H[v][i];
            if(!used[e.to] && e.cap > 0){
                ll d = dfs(e.to, t, min(f, e.cap), H);
                if(d > 0){
                    e.cap -= d;
                    H[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }

        return 0;
    }

    ll max_flow(ll s, ll t){
        ll flow = 0;
        vector<vector<Edge>> H(N);
        rep(from, N) for(Edge e : G[from]) H[from].pb(e);
        for(;;){
            rep(i, N) used[i] = false;
            ll f = dfs(s, t, INF, H);
            if(f == 0) return flow;
            flow += f;
        }
    }
};

int main(){
    ll n, m, d;
    cin >> n >> m >> d;

    vector<vector<tuple<ll, ll>>> G_in(n); // (自身のID, 到着時刻)
    vector<vector<tuple<ll, ll, ll, ll>>> G_out(n); // (自身のID, 相手のID, 出発時刻, 容量)
    ll cnt = 1;
    rep(i, m){
        ll u, v, p, q, w;
        cin >> u >> v >> p >> q >> w;
        u--; v--;
        if(u == n-1 || v == 0) continue;

        ll u_id = cnt++;
        ll v_id = cnt++;

        G_in[v].pb({v_id, q});
        G_out[u].pb({u_id, v_id, p, w});
    }

    cnt++;
    vector<vector<pll>> G(cnt);

    for(tuple<ll, ll, ll, ll> tmp : G_out[0]){
        ll v = get<0>(tmp);
        G[0].pb({v, INF});
    }

    for(ll i=1; i<n-1; i++){
        for(tuple<ll, ll> tmp1 : G_in[i]){
            ll u = get<0>(tmp1);
            ll p = get<1>(tmp1);
            for(tuple<ll, ll, ll, ll> tmp2 : G_out[i]){
                ll v = get<0>(tmp2);
                ll q = get<2>(tmp2);

                if(p + d <= q){
                    G[u].pb({v, INF});
                }
            }
        }
    }

    for(tuple<ll, ll> tmp : G_in[n-1]){
        ll u = get<0>(tmp);
        G[u].pb({cnt-1, INF});
    }

    for(ll i=0; i<n-1; i++){
        for(tuple<ll, ll, ll, ll> tmp : G_out[i]){
            ll u = get<0>(tmp);
            ll v = get<1>(tmp);
            ll c = get<3>(tmp);
            G[u].pb({v, c});
        }
    }

    /*
    for(ll i=0; i<cnt; i++){
        cout << "i : " << i << endl;
        for(pll x : G[i]){
            cout << x.fi << " " << x.se << endl;
        }
        cout << endl;
    }
    */

    Ford_Fulkerson FF(G);
    cout << FF.max_flow(0, cnt-1) << endl;
}
0