結果

問題 No.654 Air E869120
ユーザー ferinferin
提出日時 2019-10-18 13:11:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 3,481 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 195,484 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 20:24:16
合計ジャッジ時間 4,175 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;


struct dinic {
    struct edge{
        int to;
        ll cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> G;
    vector<int> level, iter;

    void bfs(int s) {
        level.assign(G.size(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(que.size()) {
            int v = que.front(); que.pop();
            for(auto i: G[v]) {
                if(i.cap > 0 && level[i.to] < 0) {
                    level[i.to] = level[v] + 1;
                    que.push(i.to);
                }
            }
        }
    }

    ll dfs(int v, const int t, ll f) {
        if(v == t) return f;
        for(edge &e: G[v]) {
            if(e.cap > 0 && level[v] < level[e.to]) {
                ll 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;
    }

    dinic() {}
    dinic(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, ll cap) {
        G[from].push_back({to, cap, (int)G[to].size(), false});
        G[to].push_back({from, 0, (int)G[from].size()-1, true});
    }

    ll max_flow(int s, int t) {
        ll flow = 0;
        while(1) {
            bfs(s);
            if(level[t] < 0) return flow;
            iter.assign(G.size(), 0);
            ll f;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
    }

    friend ostream &operator <<(ostream& out, const dinic& a){
        out << endl;
        for(int i = 0; i < (int)a.G.size(); i++) {
            for(auto &e : a.G[i]) {
                if(e.isrev) continue;
                auto &rev_e = a.G[e.to][e.rev];
                out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
        return out;
    }
};

int main(void) {
    ll n, m, d;
    cin >> n >> m >> d;
    vector<ll> u(m), v(m), p(m), q(m), w(m);
    vector<vector<ll>> t(n);
    REP(i, m) {
        cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
        u[i]--, v[i]--;
        q[i] += d;
        t[u[i]].push_back(p[i]);
        t[v[i]].push_back(q[i]);
    }

    if(t[n-1].size() == 0) {
        cout << 0 << endl;
        return 0;
    }

    ll idx = 0;
    map<PII,ll> mp;
    REP(i, n) {
        sort(ALL(t[i]));
        t[i].erase(unique(ALL(t[i])), t[i].end());
        for(auto j: t[i]) mp[{i, j}] = idx++;
    }
    dump(mp);

    dinic flow(idx);
    REP(i, m) {
        ll va = mp[{u[i], p[i]}], vb = mp[{v[i], q[i]}];
        flow.add_edge(va, vb, w[i]);   
    }
    REP(i, n) {
        FOR(j, 1, t[i].size()) {
            ll va = mp[{i, t[i][j-1]}], vb = mp[{i, t[i][j]}];
            assert(va != vb);
            flow.add_edge(va, vb, INF);
        }
    }

    cout << flow.max_flow(0, idx-1) << endl;
    // dump(flow);

    return 0;
}
0