結果

問題 No.654 Air E869120
ユーザー dsytk7dsytk7
提出日時 2018-03-27 13:23:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 3,192 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 185,448 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 18:57:17
合計ジャッジ時間 3,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

template<class T>
class Dinic {
private:
    const T INF;

    struct edge {
        T to, cap, rev;
        edge(T to, T cap, T rev) :to(to), cap(cap), rev(rev) {}
    };

    T V;
    vector<vector<edge>> g;
    vector<T> level;      // sourceからの距離
    vector<T> itr;       // どこまで調べ終わったか

public:
    Dinic(T V) :INF(1e9+7), V(V), g(V, vector<edge>()) {}

    void add_edge(T from, T to, T cap) {
        g[from].emplace_back(to, cap, g[to].size());
        g[to].emplace_back(from, 0, g[from].size()-1);
    }

    // sourceからの最短路を幅優先で探索
    void bfs(T s) {
        level = vector<T>(V, -1);
        queue<T> que;
        que.push(s);
        level[s] = 0;
        while (que.size()) {
            int v = que.front(); que.pop();
            for (edge &e : g[v]) {
                if (e.cap > 0 and level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }

    // v->tの増加路を探す
    T dfs(T v, T t, T f) {
        if (v == t) return f;
        for (T& i = itr[v]; i < g[v].size(); ++i) {
            edge& e = g[v][i];
            if (e.cap > 0 and level[v] < level[e.to]) {
                int 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 max_flow(T s, T t) {
        T flow = 0, f;
        while (true) {
            bfs(s);
            if (level[t] < 0) return flow;
            itr = vector<T>(V, 0);
            while ((f = dfs(s, t, numeric_limits<T>::max())) > 0) {
                flow += f;
            }
        }
    }
};

ll N, M, d;
ll u[1010], v[1010], p[1010], q[1010], w[1010];
ll sz[1010];
vector<ll> t[1010];

int main() {
    cin >> N >> M >> d;
    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]);
    }
    rep(i, N) {
        sort(t[i].begin(), t[i].end());
        t[i].erase(unique(t[i].begin(), t[i].end()), t[i].end());
    }
    sz[0] = t[0].size();
    for (int i=1; i<N; ++i) sz[i] = sz[i-1] + t[i].size();
    Dinic<ll> flow(sz[N-1]);
    rep(i, N) {
        int st = 0;
        if (i) st += sz[i-1];
        int n = t[i].size();
        rep(j, n-1) flow.add_edge(st+j, st+j+1, 1LL<<60);
    }
    rep(i, M) {
        int a = lower_bound(t[u[i]].begin(), t[u[i]].end(), p[i]) - t[u[i]].begin();
        if (u[i] > 0) a += sz[u[i]-1];
        int b = lower_bound(t[v[i]].begin(), t[v[i]].end(), q[i]) - t[v[i]].begin();
        if (v[i] > 0) b += sz[v[i]-1];
        flow.add_edge(a, b, w[i]);
    }
    cout << flow.max_flow(0, sz[N-1]-1) << endl;
    return 0;
}
0