結果

問題 No.957 植林
ユーザー ei1333333ei1333333
提出日時 2019-12-18 00:55:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,182 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 214,580 KB
実行使用メモリ 33,444 KB
最終ジャッジ日時 2023-09-21 07:17:31
合計ジャッジ時間 8,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,484 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 164 ms
29,008 KB
testcase_04 AC 223 ms
27,640 KB
testcase_05 AC 147 ms
29,308 KB
testcase_06 AC 261 ms
30,748 KB
testcase_07 AC 181 ms
27,308 KB
testcase_08 AC 63 ms
29,464 KB
testcase_09 AC 62 ms
29,804 KB
testcase_10 AC 67 ms
29,784 KB
testcase_11 AC 65 ms
29,236 KB
testcase_12 AC 62 ms
28,364 KB
testcase_13 AC 51 ms
26,664 KB
testcase_14 AC 62 ms
30,932 KB
testcase_15 AC 54 ms
28,100 KB
testcase_16 AC 52 ms
25,880 KB
testcase_17 AC 55 ms
26,664 KB
testcase_18 TLE -
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 #

#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);

  Wave flow(H * W + H + W + 2);
  int S = H * W + H + W;
  int T = S + 1;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      flow.add_edge(S, i * W + j, G[i][j]);
      flow.add_edge(i * W + j, H * W + i, flow.INF);
      flow.add_edge(i * W + j, H * W + H + j, flow.INF);
    }
  }
  for(int i = 0; i < H; i++) {
    flow.add_edge(H * W + i, T, R[i]);
  }
  for(int i = 0; i < W; i++) {
    flow.add_edge(H * W + H + i, T, C[i]);
  }

  cout << sum - flow.run(S, T) << endl;
}

0