結果

問題 No.957 植林
ユーザー ei1333333ei1333333
提出日時 2019-12-18 00:54:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 6,352 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 216,232 KB
実行使用メモリ 12,764 KB
最終ジャッジ日時 2023-09-21 07:16:15
合計ジャッジ時間 10,114 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 25 ms
11,312 KB
testcase_04 AC 24 ms
10,708 KB
testcase_05 AC 24 ms
11,188 KB
testcase_06 AC 31 ms
11,772 KB
testcase_07 AC 26 ms
10,968 KB
testcase_08 AC 19 ms
11,572 KB
testcase_09 AC 19 ms
11,452 KB
testcase_10 AC 20 ms
11,576 KB
testcase_11 AC 19 ms
11,244 KB
testcase_12 AC 19 ms
11,292 KB
testcase_13 AC 16 ms
9,444 KB
testcase_14 AC 19 ms
11,976 KB
testcase_15 AC 18 ms
11,240 KB
testcase_16 AC 16 ms
9,312 KB
testcase_17 AC 17 ms
10,704 KB
testcase_18 AC 198 ms
11,240 KB
testcase_19 AC 201 ms
11,496 KB
testcase_20 AC 206 ms
11,480 KB
testcase_21 AC 216 ms
11,744 KB
testcase_22 AC 223 ms
12,028 KB
testcase_23 AC 234 ms
12,088 KB
testcase_24 AC 255 ms
12,212 KB
testcase_25 AC 267 ms
12,484 KB
testcase_26 AC 263 ms
12,480 KB
testcase_27 AC 262 ms
12,612 KB
testcase_28 AC 260 ms
12,636 KB
testcase_29 AC 263 ms
12,764 KB
testcase_30 AC 261 ms
12,556 KB
testcase_31 AC 195 ms
10,952 KB
testcase_32 AC 201 ms
11,504 KB
testcase_33 AC 215 ms
11,476 KB
testcase_34 AC 216 ms
11,832 KB
testcase_35 AC 226 ms
11,984 KB
testcase_36 AC 236 ms
12,232 KB
testcase_37 AC 246 ms
12,292 KB
testcase_38 AC 256 ms
12,724 KB
testcase_39 AC 263 ms
12,624 KB
testcase_40 AC 262 ms
12,680 KB
testcase_41 AC 17 ms
12,532 KB
testcase_42 AC 16 ms
12,508 KB
testcase_43 AC 23 ms
12,716 KB
testcase_44 AC 27 ms
12,716 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}


#define rep(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, b, n) for(int i = (b); i < (n); ++i)
#define let(v, x) __typeof(x) v = (x)
#define foreach(i, v) for(let(i, (v).begin());i!=(v).end();i++)


struct Wave {//{{{
  typedef int64 Cap;
  static const Cap INF = 1LL << 60;

  struct E {//{{{
    int src, dst;
    Cap cap, _cap;
    int rev;

    E(int src, int dst, Cap cap, int rev) :
        src(src), dst(dst), cap(cap), _cap(0), rev(rev) {}
  };//}}}
  int n;
  vector< vector< E > > g;

  Wave(int n) : n(n), g(n) {}

  inline void add_edge(int src, int dst, Cap cap) {//{{{
    if(src == dst) return;
    g[src].push_back(E(src, dst, cap, g[dst].size()));
    g[dst].push_back(E(dst, src, 0, g[src].size() - 1));
  }//}}}

  inline E &rev(const E &e) { return g[e.dst][e.rev]; }

  vector< int > level, iter;
  vector< int > blocked;
  vector< int > unbalance[2]; // { unblocked, blocked }
  vector< int > inS;
  vector< Cap > ex;

  inline void push(E &e) {//{{{
    Cap f = min(e.cap, ex[e.src]);
    if(!inS[e.dst]) unbalance[blocked[e.dst]].push_back(e.dst);
    inS[e.dst] = true;
    e.cap -= f;
    rev(e).cap += f;
    ex[e.src] -= f;
    ex[e.dst] += f;
  }//}}}
  // dir = +1 ? s to t : t to s
  inline void discharge(const int &u, const int dir) {//{{{
    for(int &i = iter[u]; i < g[u].size(); ++i) {
      E &e = g[u][i];
      if(e.cap == 0 || level[e.src] + dir != level[e.dst]) continue;
      if(dir == +1 && blocked[e.dst]) continue;
      push(e);
      if(ex[u] == 0) return;
    }
    blocked[u] = true;
    if(!inS[u]) unbalance[1].push_back(u);
    inS[u] = true;
    iter[u] = 0;
  }//}}}
  Cap run(int s, int t) {//{{{
    vector< int > q(n);
    vector< int > tmp;
    tmp.reserve(n);
    rep(i, 2) unbalance[i].reserve(n);
    rep(i, 2) unbalance[i].clear();
    inS.assign(n, false);
    inS[s] = inS[t] = true;
    for(Cap flow = 0;;) {
      level.assign(n, -1);
      int ql = 0, qr = 0;
      level[q[qr++] = s] = 0;
      while(ql != qr && level[t] == -1) {
        const int &u = q[ql++];
        foreach(e, g[u]) if(level[e->dst] == -1 && e->cap + e->_cap > 0)
            level[q[qr++] = e->dst] = level[u] + 1;
      }
      if(level[t] == -1) return flow;

      rep(i, qr) {
        if(level[q[i]] == level[t] && q[i] != t) {
          level[q[i]] = -1;
          continue;
        }
        foreach(e, g[q[i]]) {
          if(level[e->src] + 1 == level[e->dst]) {
            e->cap += e->_cap;
            e->_cap = 0;
          } else {
            e->_cap += e->cap;
            e->cap = 0;
          }
        }
      }

      iter.assign(n, 0);
      blocked.assign(n, false);
      ex.assign(n, 0);
      ex[s] = INF;
      discharge(s, +1);
      ex[s] = 0;

      while(!unbalance[0].empty() || !unbalance[1].empty()) {
        rep(b, 2) while(!unbalance[b].empty()) {
            tmp.clear();
            int l = level[unbalance[b].back()];
            while(!unbalance[b].empty() && level[unbalance[b].back()] == l) {
              int v = unbalance[b].back();
              unbalance[b].pop_back();
              inS[v] = false;
              tmp.push_back(v);
            }
            foreach(v, tmp) discharge(*v, b ? -1 : +1);
          }
      }
      flow += ex[t];
    }
  }//}}}
};//}}}


int main() {
  int H, W;
  cin >> H >> W;
  vector< vector< int64 > > G(H, vector< int64 >(W));
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) cin >> G[i][j];
  }
  vector< int64 > R(H), C(W);
  for(int i = 0; i < H; i++) cin >> R[i];
  for(int i = 0; i < W; i++) cin >> C[i];

  int64 sum = accumulate(begin(R), end(R), 0LL) + accumulate(begin(C), end(C), 0LL);

  vector< int64 > row_sum(H);
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) row_sum[i] += G[i][j];
  }
  int64 sum_cut = 0;
  vector< int64 > row_cap(H);
  for(int i = 0; i < H; i++) {
    int64 cut = min(R[i], row_sum[i]);
    row_cap[i] = row_sum[i] - cut;
    sum_cut += cut;
  }

  Wave flow(H + W + 2);
  int S = H + W;
  int T = S + 1;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      flow.add_edge(i, H + j, G[i][j]);
    }
  }
  for(int i = 0; i < H; i++) {
    flow.add_edge(S, i, row_cap[i]);
  }
  for(int i = 0; i < W; i++) {
    flow.add_edge(H + i, T, C[i]);
  }
  cout << sum - flow.run(S, T) - sum_cut << endl;
}


0