結果

問題 No.957 植林
ユーザー nebukuro09nebukuro09
提出日時 2019-12-20 14:19:43
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,649 bytes
コンパイル時間 674 ms
コンパイル使用メモリ 115,552 KB
実行使用メモリ 11,492 KB
最終ジャッジ日時 2023-09-04 04:23:27
合計ジャッジ時間 4,498 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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, core.stdc.string;

void main(){
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = iota(H).map!(_ => readln.split.map!(to!long).array).array;
    auto R = readln.split.map!(to!long).array;
    auto C = readln.split.map!(to!long).array;

    int source = H + W;
    int sink = H + W + 1;
    auto ff = new Dinic(H+W+2, source, sink);

    foreach (i; 0..H) {
        ff.add_edge(source, i, A[i].sum);
        ff.add_edge(i, sink, R[i]);
    }


    foreach (j; 0..W) {
        ff.add_edge(H+j, sink, C[j]);
    }

    foreach (i; 0..H) foreach (j; 0..W) {
        ff.add_edge(i, H+j, A[i][j]);
    }

    writeln(R.sum + C.sum - ff.run);
}

class Dinic {
    import std.typecons : Tuple;
    import std.conv : to;
    import std.container : DList;
    import std.algorithm : min;

    alias Edge = Tuple!(int, "to", long, "cap", long, "rev");
    immutable long INF = 1L << 59;

    int V, source, sink;
    Edge[][] G;
    int[] itr, level;

    this(int V, int s, int t) {
        this.V = V;
        G = new Edge[][](V);
        source = s;
        sink = t;
    }

    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[] = -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) {
            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() {
        long ret = 0, f;
        while (true) {
            bfs(source);
            if (level[sink] < 0) break;
            itr = new int[](V);
            while ((f = dfs(source, sink, INF)) > 0) ret += f;
        }
        return ret;
    }
}
0