結果

問題 No.957 植林
ユーザー niuezniuez
提出日時 2020-06-12 14:22:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 5,442 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 185,428 KB
実行使用メモリ 12,976 KB
最終ジャッジ日時 2023-09-06 09:12:04
合計ジャッジ時間 9,881 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 54 ms
8,232 KB
testcase_04 AC 225 ms
9,432 KB
testcase_05 AC 11 ms
8,104 KB
testcase_06 AC 560 ms
12,976 KB
testcase_07 AC 46 ms
8,496 KB
testcase_08 AC 9 ms
7,972 KB
testcase_09 AC 9 ms
8,124 KB
testcase_10 AC 9 ms
8,168 KB
testcase_11 AC 9 ms
7,916 KB
testcase_12 AC 12 ms
7,980 KB
testcase_13 AC 8 ms
7,620 KB
testcase_14 AC 9 ms
8,448 KB
testcase_15 AC 8 ms
7,644 KB
testcase_16 AC 9 ms
7,392 KB
testcase_17 AC 8 ms
7,528 KB
testcase_18 AC 106 ms
9,224 KB
testcase_19 AC 112 ms
9,736 KB
testcase_20 AC 117 ms
9,812 KB
testcase_21 AC 121 ms
9,816 KB
testcase_22 AC 125 ms
10,028 KB
testcase_23 AC 131 ms
10,332 KB
testcase_24 AC 136 ms
10,272 KB
testcase_25 AC 141 ms
10,600 KB
testcase_26 AC 141 ms
10,492 KB
testcase_27 AC 143 ms
10,604 KB
testcase_28 AC 141 ms
10,668 KB
testcase_29 AC 141 ms
10,680 KB
testcase_30 AC 141 ms
10,624 KB
testcase_31 AC 106 ms
9,284 KB
testcase_32 AC 112 ms
9,808 KB
testcase_33 AC 117 ms
9,664 KB
testcase_34 AC 121 ms
9,888 KB
testcase_35 AC 125 ms
10,020 KB
testcase_36 AC 130 ms
10,276 KB
testcase_37 AC 135 ms
10,328 KB
testcase_38 AC 141 ms
10,540 KB
testcase_39 AC 142 ms
10,632 KB
testcase_40 AC 149 ms
10,668 KB
testcase_41 AC 8 ms
8,840 KB
testcase_42 AC 8 ms
8,744 KB
testcase_43 AC 10 ms
9,000 KB
testcase_44 AC 16 ms
8,696 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <bitset>
#include <tuple>

struct residual_graph {
  using cap_type = long long;
  struct edge {
    int to;
    cap_type cap;
    int rev;
    edge(int t = 0, cap_type c = 0, int r = 0): to(t), cap(c), rev(r) {}
  };
  int N;
  std::vector<int> iter;
  std::vector<std::tuple<int, int, cap_type>> pool;
  std::vector<edge> edges;
  residual_graph(int N): N(N), iter(N + 1) {}

  void add_edge(int from, int to, cap_type cap, cap_type rev_cap) {
    iter[from]++;
    iter[to]++;
    pool.emplace_back(from, to, cap);
  }
  void build() {
    for(int i = 0; i < N; i++) {
      iter[i + 1] += iter[i];
    }
    edges.resize(pool.size() * 2);
    for(auto&& p: pool) {
      int fi = --iter[std::get<0>(p)];
      int ti = --iter[std::get<1>(p)];
      edges[fi] = edge(std::get<1>(p), std::get<2>(p), ti);
      edges[ti] = edge(std::get<0>(p), 0, fi);
    }
  }
  inline edge& operator[](int i) { return edges[i]; }
};

struct goldberg_tarjan {
  using cap_type = long long;
  struct edge {
    int to;
    cap_type cap;
    edge(int t, cap_type c, int r): to(t), cap(c) {}
  };

  int N;
  int H_MAX;
  residual_graph G;
  std::vector<cap_type> exc;
  std::vector<int> h;
  std::vector<std::vector<int>> que;
  std::vector<int> qi;
  std::vector<int> ei;
  std::vector<int> hcnt;
  std::bitset<202020> inque;
  const cap_type INF = 1e18;

  goldberg_tarjan(int n): N(n), H_MAX(n * 2), G(n) {}
  
  void add_edge(int from, int to, cap_type cap, cap_type rev_cap = 0) {
    G.add_edge(from, to, cap, rev_cap);
  }
  void build() {
    G.build();
  }

  void push(int from, int ei) {
    cap_type fl = std::min(exc[from], G[ei].cap);
    G[ei].cap -= fl;
    G[G[ei].rev].cap += fl;
    exc[from] -= fl;
    exc[G[ei].to] += fl;
  }

  void relabel(int v) {
    hcnt[h[v]]--;
    h[v] = H_MAX;
    for(int i = G.iter[v]; i < G.iter[v + 1]; i++) {
      auto& e = G[i];
      if(e.cap > 0 && h[v] > h[e.to] + 1) {
        ei[v] = i;
        h[v] = h[e.to] + 1;
      }
    }
    hcnt[h[v]]++;
  }

  int global_relabeling(int t) {
    std::fill(std::begin(h), std::end(h), H_MAX);
    std::fill(std::begin(hcnt), std::end(hcnt), 0);
    for(int i = 0;i < H_MAX;i++) {
      que[i].clear();
    }
    inque.reset();
    int i = 0, qr = 0;
    std::vector<int>& Q = qi;
    Q[qr++] = t;
    h[t] = 0;
    int hi = 0;
    while(i < qr) {
      int v = Q[i++];
      hi = h[v];
      hcnt[hi]++;
      if(exc[v] > 0 && v != t) {
        que[h[v]].emplace_back(v);
        inque.set(v);
      }
      for(int gi = G.iter[v]; gi < G.iter[v + 1]; gi++) {
        auto& e = G[gi];
        if(G[e.rev].cap > 0 && h[v] + 1 < h[e.to]) {
          h[e.to] = h[v] + 1;
          Q[qr++] = e.to;
        }
      }
    }
    std::copy(std::begin(G.iter), std::begin(G.iter) + N, std::begin(ei));
    std::fill(std::begin(qi), std::end(qi), 0);
    return hi;
  }

  cap_type max_flow(int s, int t) {
    exc.assign(H_MAX, 0);
    exc[s] = INF;
    h.assign(H_MAX, 0);

    que.resize(H_MAX);
    qi.assign(H_MAX, 0);
    ei.resize(H_MAX);
    hcnt.assign(H_MAX + 1, 0);

    global_relabeling(t);

    if(h[s] == H_MAX) return 0;

    for(int di = h[s]; di >= 0;) {
      if(qi[di] == que[di].size()) { di--; continue; }
      int v = que[di][qi[di]++];
      inque.reset(v);
      if(exc[v] == 0 || v == t) continue;
      for(int& i = ei[v]; i < G.iter[v + 1]; i++) {
        auto& e = G[i];
        if(e.cap > 0 && h[v] == h[e.to] + 1) {
          push(v, i);
          if(exc[e.to] > 0 && e.to != t && !inque.test(e.to)) {
            que[h[e.to]].emplace_back(e.to);
            inque.set(e.to);
          }
          if(exc[v] == 0) break;
        }
      }
      if(exc[v] == 0) continue;
      relabel(v);
      if(h[v] < H_MAX) {
        di = h[v];
        que[h[v]].emplace_back(v);
        inque.set(v);
      }
    }
    return exc[t];
  }
};

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

template<class T, class Cond>
struct chain {
  Cond cond; chain(Cond cond) : cond(cond) {}
  bool operator()(T& a, const T& b) const {
    if(cond(a, b)) { a = b; return true; }
    return false;
  }
};
template<class T, class Cond>
chain<T, Cond> make_chain(Cond cond) { return chain<T, Cond>(cond); }

#ifndef LOCAL
#define getchar getchar_unlocked
#endif

int getint() {
    char c;
    while (not isdigit(c = getchar()))
        ;
    int res = c - '0';
    while (isdigit(c = getchar())) res = res * 10 + (c - '0');
    return res;
}


int main() {
  int H = getint(), W = getint();

  goldberg_tarjan gt(H + W + 2);
  int s = H + W;
  int t = s + 1;
  vector<i64> A(H, 0);
  rep(i,0,H) rep(j,0,W) {
    int g = getint();
    A[i] += g;
    gt.add_edge(i, H + j, g);
  }
  i64 sum = 0;
  rep(i,0,H) {
    i64 r = getint();
    i64 MIN = std::min(A[i], r);
    sum += r - MIN;
    gt.add_edge(s, i, A[i] - MIN);
  }
  rep(i,0,W) {
    i64 r = getint();
    sum += r;
    gt.add_edge(H + i, t, r);
  }
  gt.build();
  cout << sum - gt.max_flow(s, t) << "\n";
}
0