結果

問題 No.957 植林
ユーザー niuezniuez
提出日時 2020-05-10 11:31:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 3,955 bytes
コンパイル時間 1,847 ms
コンパイル使用メモリ 180,688 KB
実行使用メモリ 16,456 KB
最終ジャッジ日時 2023-09-21 17:48:50
合計ジャッジ時間 7,813 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 29 ms
9,400 KB
testcase_04 AC 36 ms
8,912 KB
testcase_05 AC 20 ms
9,980 KB
testcase_06 AC 44 ms
10,288 KB
testcase_07 AC 27 ms
9,168 KB
testcase_08 AC 15 ms
9,572 KB
testcase_09 AC 16 ms
9,436 KB
testcase_10 AC 16 ms
9,936 KB
testcase_11 AC 15 ms
9,444 KB
testcase_12 AC 16 ms
9,792 KB
testcase_13 AC 12 ms
7,972 KB
testcase_14 AC 16 ms
9,708 KB
testcase_15 AC 15 ms
9,580 KB
testcase_16 AC 13 ms
8,296 KB
testcase_17 AC 13 ms
8,948 KB
testcase_18 AC 132 ms
14,288 KB
testcase_19 AC 136 ms
14,060 KB
testcase_20 AC 143 ms
15,140 KB
testcase_21 AC 151 ms
15,328 KB
testcase_22 AC 156 ms
15,640 KB
testcase_23 AC 161 ms
15,280 KB
testcase_24 AC 171 ms
15,372 KB
testcase_25 AC 173 ms
15,692 KB
testcase_26 AC 166 ms
15,244 KB
testcase_27 AC 181 ms
16,456 KB
testcase_28 AC 178 ms
15,484 KB
testcase_29 AC 177 ms
15,964 KB
testcase_30 AC 174 ms
15,756 KB
testcase_31 AC 133 ms
14,004 KB
testcase_32 AC 138 ms
13,996 KB
testcase_33 AC 142 ms
15,260 KB
testcase_34 AC 151 ms
15,120 KB
testcase_35 AC 152 ms
15,644 KB
testcase_36 AC 158 ms
15,280 KB
testcase_37 AC 164 ms
15,304 KB
testcase_38 AC 168 ms
15,680 KB
testcase_39 AC 165 ms
15,104 KB
testcase_40 AC 175 ms
16,320 KB
testcase_41 AC 11 ms
9,852 KB
testcase_42 AC 12 ms
9,904 KB
testcase_43 AC 19 ms
9,904 KB
testcase_44 AC 21 ms
10,624 KB
testcase_45 AC 1 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>

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

  int N;
  std::vector<std::vector<edge>> G;
  std::vector<cap_type> exc;
  std::vector<int> h;
  std::vector<std::vector<int>> que;
  std::vector<int> qi;
  std::vector<int> ei;
  const cap_type INF = 1e18;

  goldberg_tarjan(int n): N(n), G(n) {}
  
  void add_edge(int from, int to, cap_type cap, cap_type rev_cap = 0) {
    G[from].emplace_back(to, cap, (int)(G[to].size()));
    G[to].emplace_back(from, rev_cap, (int)(G[from].size() - 1));
  }

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

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

  std::vector<int> Q;
  int global_relabeling(int t) {
    for(int i = 0;i < N;i++) {
      que[i].clear();
      qi[i] = 0;
      ei[i] = 0;
    }
    int i = 0, qr = 0;
    Q[qr++] = t;
    h.assign(N, N);
    h[t] = 0;
    int hi = 0;
    while(i < qr) {
      int v = Q[i++];
      hi = h[v];
      if(exc[v] > 0 && v != t) {
        que[h[v]].push_back(v);
      }
      for(const auto& e: G[v]) {
        if(G[e.to][e.rev].cap > 0 && h[v] + 1 < h[e.to]) {
          h[e.to] = h[v] + 1;
          Q[qr++] = e.to;
        }
      }
    }
    return hi;
  }

  cap_type max_flow(int s, int t) {
    exc.assign(N, 0);
    exc[s] = INF;
    h.assign(N, 0);
    Q.resize(N);
    int cnt = 0;

    que.assign(N, std::vector<int>());
    qi.assign(N, 0);
    ei.assign(N, 0);

    global_relabeling(t);

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

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

      if(++cnt % (N / 2) == 0) {
        di = global_relabeling(t);
      }
    }
    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); }

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;

  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) {
    i64 g;
    cin >> g;
    A[i] += g;
    gt.add_edge(i, H + j, g);
  }
  i64 sum = 0;
  rep(i,0,H) {
    i64 r;
    cin >> r;
    i64 MIN = std::min(A[i], r);
    sum += r - MIN;
    gt.add_edge(s, i, A[i] - MIN);
  }
  rep(i,0,W) {
    i64 r;
    cin >> r;
    sum += r;
    gt.add_edge(H + i, t, r);
  }
  cout << sum - gt.max_flow(s, t) << "\n";
}
0