結果

問題 No.654 Air E869120
ユーザー nebukuro09nebukuro09
提出日時 2018-02-23 23:44:41
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 2,703 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 114,068 KB
実行使用メモリ 11,312 KB
最終ジャッジ日時 2023-09-03 18:48:50
合計ジャッジ時間 3,773 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

alias Plane = Tuple!(int, "u", int, "v", int, "p", int, "q", int, "w");
immutable long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto D = s[2];
    auto P = new Plane[](M);
    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        P[i] = Plane(s[0], s[1], s[2], s[3], s[4]);
    }

    int source = M;
    int sink = M + 1;
    auto dinic = new Dinic(M + 2);

    foreach (i; 0..M) {
        if (P[i].u == 1) {
            dinic.add_edge(source, i, P[i].w);
        }
        if (P[i].v == N) {
            dinic.add_edge(i, sink, P[i].w);
        }
    }

    foreach (i; 0..M) {
        foreach (j; 0..M) {
            if (i == j) continue;
            if (P[i].v == P[j].u && P[j].p - P[i].q >= D) {
                dinic.add_edge(i, j, min(P[i].w, P[j].w));
            }
        }
    }

    dinic.run(source, sink).writeln;
}

class Dinic {
    alias Edge = Tuple!(int, "to", long, "cap", long, "rev");
    int V;
    Edge[][] G;
    int[] itr, level;

    this(int V) {
        this.V = V;
        G = new Edge[][](V);
    }

    void add_edge(int from, int to, long cap) {
        G[from] ~= Edge(to, cap, G[to].length.to!long);
        G[to] ~= Edge(from, 0, G[from].length.to!long-1);
    }

    void bfs(int s) {
        level = new int[](V);
        level.fill(-1);
        DList!int q;
        level[s] = 0;
        q.insertBack(s);
        while (!q.empty) {
            int v = q.front();
            q.removeFront();
            foreach (e; G[v]){
                if (e.cap > 0 && level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    q.insertBack(e.to);
                }
            }
        }
    }

    long dfs(int v, int t, long f) {
        if (v == t) return f;
        for (int i = itr[v]; i < G[v].length.to!int; ++i) {
            //auto e = G[v][i];
            if (G[v][i].cap > 0 && level[v] < level[G[v][i].to]) {
                long d = dfs(G[v][i].to, t, min(f, G[v][i].cap));
                if (d > 0) {
                    G[v][i].cap -= d;
                    G[G[v][i].to][G[v][i].rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    long run(int s, int t) {
        long ret = 0, f;
        while (true) {
            bfs(s);
            if (level[t] < 0) break;
            itr = new int[](V);
            while ((f = dfs(s, t, INF)) > 0) ret += f;
        }
        return ret;
    }
}
0