結果

問題 No.654 Air E869120
ユーザー finefine
提出日時 2018-02-24 01:55:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,342 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 170,724 KB
実行使用メモリ 37,888 KB
最終ジャッジ日時 2024-04-19 05:21:50
合計ジャッジ時間 4,460 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
34,696 KB
testcase_01 AC 19 ms
34,816 KB
testcase_02 AC 17 ms
34,688 KB
testcase_03 AC 17 ms
34,944 KB
testcase_04 AC 17 ms
34,944 KB
testcase_05 AC 18 ms
34,944 KB
testcase_06 AC 18 ms
34,868 KB
testcase_07 AC 18 ms
34,816 KB
testcase_08 AC 17 ms
34,688 KB
testcase_09 AC 19 ms
34,936 KB
testcase_10 AC 30 ms
37,888 KB
testcase_11 AC 37 ms
36,608 KB
testcase_12 AC 38 ms
37,120 KB
testcase_13 AC 37 ms
36,992 KB
testcase_14 AC 36 ms
36,736 KB
testcase_15 AC 36 ms
37,532 KB
testcase_16 AC 29 ms
35,804 KB
testcase_17 AC 31 ms
35,964 KB
testcase_18 AC 30 ms
35,712 KB
testcase_19 AC 31 ms
35,840 KB
testcase_20 AC 32 ms
35,448 KB
testcase_21 AC 31 ms
35,584 KB
testcase_22 AC 28 ms
35,452 KB
testcase_23 AC 28 ms
35,328 KB
testcase_24 AC 27 ms
35,328 KB
testcase_25 AC 26 ms
35,328 KB
testcase_26 AC 26 ms
35,328 KB
testcase_27 AC 25 ms
35,232 KB
testcase_28 AC 26 ms
35,272 KB
testcase_29 AC 25 ms
35,200 KB
testcase_30 AC 20 ms
30,976 KB
testcase_31 AC 20 ms
31,232 KB
testcase_32 AC 22 ms
31,104 KB
testcase_33 AC 21 ms
31,360 KB
testcase_34 AC 20 ms
31,232 KB
testcase_35 AC 21 ms
34,816 KB
testcase_36 AC 21 ms
34,944 KB
testcase_37 AC 21 ms
34,688 KB
testcase_38 AC 19 ms
30,848 KB
testcase_39 AC 23 ms
34,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MAX_V = 1000005;
const ll INF = 10000000000000LL;

struct Edge {
    int to;
    ll cap;
    int rev;  //行き先、容量、逆辺
    Edge(int to, ll cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<Edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

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

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

ll dfs(int v, int t, ll f) {
    if (v == t) return f;
    for (int& i = iter[v]; i < G[v].size(); i++) {
        Edge& e = G[v][i];
        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;
}

ll max_flow(int s, int t) {
    ll flow = 0;
    while (true) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof(iter));
        ll f;
        while ((f = dfs(s, t, INF)) > 0) {
            flow += f;
        }
    }
}

int u[1005], v[1005];
ll p[1005], q[1005], w[1005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    ll d;
    cin >> n >> m >> d;
    for (int i = 0; i < m; i++) {
        cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
        u[i]--; v[i]--;
    }

    for (int i = 1; i <= m; i++) add_edge(0, i, INF);
    for (int i = 0; i < m; i++) {
        add_edge(u[i] * m + i + 1, v[i] * m + i + 1, w[i]);
        for (int j = 0; j < m; j++) {
            if (v[j] == u[i] && q[j] + d <= p[i]) {
                add_edge(v[j] * m + j + 1, u[i] * m + i + 1, w[j]);
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        add_edge((n - 1) * m + i, n * m + 1, INF);
    }
    cout << max_flow(0, n * m + 1) << endl;
    return 0;
}
0