結果

問題 No.957 植林
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-20 01:45:21
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,093 ms / 2,000 ms
コード長 3,837 bytes
コンパイル時間 1,108 ms
コンパイル使用メモリ 121,364 KB
実行使用メモリ 11,536 KB
最終ジャッジ日時 2024-06-22 03:55:30
合計ジャッジ時間 26,398 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 72 ms
11,036 KB
testcase_04 AC 64 ms
7,336 KB
testcase_05 AC 69 ms
7,884 KB
testcase_06 AC 79 ms
7,976 KB
testcase_07 AC 67 ms
7,524 KB
testcase_08 AC 55 ms
7,596 KB
testcase_09 AC 59 ms
7,508 KB
testcase_10 AC 64 ms
7,948 KB
testcase_11 AC 60 ms
7,516 KB
testcase_12 AC 57 ms
7,484 KB
testcase_13 AC 51 ms
10,440 KB
testcase_14 AC 65 ms
8,032 KB
testcase_15 AC 55 ms
7,476 KB
testcase_16 AC 50 ms
9,704 KB
testcase_17 AC 51 ms
7,324 KB
testcase_18 AC 809 ms
8,584 KB
testcase_19 AC 855 ms
8,704 KB
testcase_20 AC 856 ms
8,860 KB
testcase_21 AC 910 ms
9,944 KB
testcase_22 AC 953 ms
10,312 KB
testcase_23 AC 979 ms
10,388 KB
testcase_24 AC 997 ms
10,048 KB
testcase_25 AC 1,089 ms
10,968 KB
testcase_26 AC 1,093 ms
11,496 KB
testcase_27 AC 1,072 ms
9,912 KB
testcase_28 AC 1,070 ms
10,092 KB
testcase_29 AC 1,080 ms
10,436 KB
testcase_30 AC 1,056 ms
10,872 KB
testcase_31 AC 782 ms
8,584 KB
testcase_32 AC 828 ms
9,884 KB
testcase_33 AC 833 ms
8,884 KB
testcase_34 AC 883 ms
10,008 KB
testcase_35 AC 937 ms
9,412 KB
testcase_36 AC 962 ms
10,352 KB
testcase_37 AC 997 ms
9,384 KB
testcase_38 AC 1,084 ms
10,116 KB
testcase_39 AC 1,081 ms
9,540 KB
testcase_40 AC 1,066 ms
10,228 KB
testcase_41 AC 61 ms
10,836 KB
testcase_42 AC 61 ms
11,536 KB
testcase_43 AC 73 ms
8,048 KB
testcase_44 AC 74 ms
8,112 KB
testcase_45 AC 1 ms
6,940 KB
testcase_46 AC 1 ms
6,944 KB
testcase_47 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

class MaxFlow(Capa) {
  enum Capa wEPS = 0;
  enum Capa wINF = 10L^^18;
  int n, m;
  int[][] g;
  int[] zu;
  Capa[] capa;
  Capa tof;
  int[] lev, see, que;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; zu = []; capa = [];
    lev = new int[n]; see = new int[n]; que = new int[n];
  }
  void addEdge(int u, int v, Capa w0, Capa w1 = 0) {
    g[u] ~= m; zu ~= v; capa ~= w0; ++m;
    g[v] ~= m; zu ~= u; capa ~= w1; ++m;
  }
  Capa augment(int src, int ink, Capa flo) {
    if (src == ink) return flo;
    foreach (i; g[src][see[src] .. $]) {
      if (capa[i] > wEPS && lev[src] < lev[zu[i]]) {
        Capa f = augment(zu[i], ink, min(flo, capa[i]));
        if (f > wEPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
      ++see[src];
    }
    return 0;
  }
  bool dinic(int src, int ink, Capa flo = wINF) {
    for (tof = 0; tof + wEPS < flo; ) {
      int[] q;
      lev[] = -1;
     dinicBFS:
      for (lev[src] = 0, q ~= src; !q.empty; ) {
        int u = q.front; q.popFront;
        foreach (i; g[u]) {
          int v = zu[i];
          if (capa[i] > wEPS && lev[v] == -1) {
            lev[v] = lev[u] + 1; q ~= v;
            if (v == ink) break dinicBFS;
          }
        }
      }
      if (lev[ink] == -1) return false;
      see[] = 0;
      for (; ; ) {
        Capa f = augment(src, ink, flo - tof);
        if (f <= wEPS) break;
        tof += f;
      }
    }
    return true;
  }
}

/*
  cost = \sum_x -R[x] a_x + \sum_y -C[y] b_y + \sum_{x,y} G[x][y] (a_x OR b_y)
  00 -> 0
  01 -> 1
  10 -> 1
  11 -> 1
  submodular
  
*/
void main() {
  try {
    for (; ; ) {
      const H = readInt();
      const W = readInt();
      auto G = new long[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        G[x][y] = readLong();
      }
      auto R = new long[H];
      foreach (x; 0 .. H) {
        R[x] = readLong();
      }
      auto C = new long[W];
      foreach (y; 0 .. W) {
        C[y] = readLong();
      }
      
      auto mf = new MaxFlow!long(2 + H + W);
      auto sums = new long[H];
      long ans;
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        sums[x] += G[x][y];
        mf.addEdge(2 + x, 2 + H + y, G[x][y]);
      }
      foreach (x; 0 .. H) {
        if (sums[x] - R[x] > 0) {
          mf.addEdge(0, 2 + x, sums[x] - R[x]);
        } else if (sums[x] - R[x] < 0) {
          ans += sums[x] - R[x];
          mf.addEdge(2 + x, 1, R[x] - sums[x]);
        }
      }
      foreach (y; 0 .. W) {
        ans += -C[y];
        mf.addEdge(2 + H + y, 1, C[y]);
      }
      mf.dinic(0, 1);
      ans += mf.tof;
      ans *= -1;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0