結果

問題 No.957 植林
ユーザー 👑 emthrmemthrm
提出日時 2019-12-20 02:06:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,628 ms / 2,000 ms
コード長 8,215 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 144,192 KB
実行使用メモリ 40,560 KB
最終ジャッジ日時 2024-07-07 03:08:54
合計ジャッジ時間 36,997 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 88 ms
32,020 KB
testcase_04 AC 98 ms
29,856 KB
testcase_05 AC 77 ms
33,404 KB
testcase_06 AC 125 ms
35,108 KB
testcase_07 AC 82 ms
31,564 KB
testcase_08 AC 51 ms
33,576 KB
testcase_09 AC 50 ms
33,504 KB
testcase_10 AC 54 ms
33,880 KB
testcase_11 AC 52 ms
33,316 KB
testcase_12 AC 53 ms
33,012 KB
testcase_13 AC 41 ms
30,892 KB
testcase_14 AC 51 ms
35,516 KB
testcase_15 AC 47 ms
31,840 KB
testcase_16 AC 43 ms
29,884 KB
testcase_17 AC 44 ms
30,140 KB
testcase_18 AC 1,131 ms
32,292 KB
testcase_19 AC 1,194 ms
33,348 KB
testcase_20 AC 1,222 ms
33,800 KB
testcase_21 AC 1,306 ms
35,644 KB
testcase_22 AC 1,333 ms
35,840 KB
testcase_23 AC 1,390 ms
36,128 KB
testcase_24 AC 1,467 ms
37,516 KB
testcase_25 AC 1,548 ms
37,840 KB
testcase_26 AC 1,580 ms
38,144 KB
testcase_27 AC 1,570 ms
39,512 KB
testcase_28 AC 1,582 ms
40,560 KB
testcase_29 AC 1,569 ms
38,372 KB
testcase_30 AC 1,573 ms
38,544 KB
testcase_31 AC 1,119 ms
31,872 KB
testcase_32 AC 1,203 ms
33,388 KB
testcase_33 AC 1,236 ms
33,520 KB
testcase_34 AC 1,312 ms
34,620 KB
testcase_35 AC 1,373 ms
36,060 KB
testcase_36 AC 1,400 ms
36,036 KB
testcase_37 AC 1,477 ms
38,048 KB
testcase_38 AC 1,625 ms
38,428 KB
testcase_39 AC 1,564 ms
38,196 KB
testcase_40 AC 1,628 ms
37,940 KB
testcase_41 AC 48 ms
39,992 KB
testcase_42 AC 49 ms
37,668 KB
testcase_43 AC 63 ms
38,428 KB
testcase_44 AC 74 ms
37,944 KB
testcase_45 AC 2 ms
6,944 KB
testcase_46 AC 1 ms
6,940 KB
testcase_47 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
// https://github.com/ei1333/library/blob/master/graph/flow/push-relabel.cpp
class Stack {
private:
  const int N, H;
  vector< int > node;

public:
  Stack(const int N, const int H) : N(N), H(H), node(N + H) { clear(); }

  inline bool empty(const int h) const { return node[N + h] == N + h; }

  inline int top(const int h) const { return node[N + h]; }

  inline void pop(const int h) { node[N + h] = node[node[N + h]]; }

  inline void push(const int h, const int u) { node[u] = node[N + h], node[N + h] = u; }

  inline void clear() { iota(node.begin() + N, node.end(), N); }
};

class List {
public:
  struct node {
    int prev, next;
  };
  const int N, H;
  vector< node > dat;

  List(const int N, const int H) : N(N), H(H), dat(N + H) { clear(); }

  inline bool empty(const int h) const { return (dat[N + h].next == N + h); }

  inline bool more_one(const int h) const { return dat[N + h].prev != dat[N + h].next; }

  inline void insert(const int h, const int u) {
    dat[u].prev = dat[N + h].prev, dat[u].next = N + h;
    dat[dat[N + h].prev].next = u, dat[N + h].prev = u;
  }

  inline void erase(const int u) {
    dat[dat[u].prev].next = dat[u].next, dat[dat[u].next].prev = dat[u].prev;
  }

  inline void clear() {
    for(int i = N; i < N + H; ++i) dat[i].prev = dat[i].next = i;
  }
};

template< typename flow_t >
struct PushRelabel {
  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
    int idx;
  };

  vector< vector< edge > > graph;
  vector< int > potential, cur_edge;
  vector< flow_t > ex;
  int V, height, relabels;
  List all_ver;
  Stack act_ver;

  PushRelabel(int V)
      : V(V), height(-1), relabels(0), ex(V, flow_t(0)), potential(V, 0), cur_edge(V), all_ver(V, V), act_ver(V, V), graph(V) {}

  void add_edge(int from, int to, flow_t cap, int idx = -1) {
    graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});
    graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});
  }

  int calc_active(int t) {
    height = -1;
    for(int i = 0; i < V; i++) {
      if(potential[i] < V) {
        cur_edge[i] = 0;
        height = max(height, potential[i]);
        all_ver.insert(potential[i], i);
        if(ex[i] > 0 && i != t) act_ver.push(potential[i], i);
      } else {
        potential[i] = V + 1;
      }
    }
    return height;
  }

  void bfs(int t) {
    for(int i = 0; i < V; i++) {
      potential[i] = max(potential[i], V);
    }
    potential[t] = 0;
    queue< int > que;
    que.emplace(t);
    while(!que.empty()) {
      int p = que.front();
      que.pop();
      for(auto &e : graph[p]) {
        if(potential[e.to] == V && graph[e.to][e.rev].cap > 0) {
          potential[e.to] = potential[p] + 1;
          que.emplace(e.to);
        }
      }
    }
  }

  int init(int s, int t) {
    potential[s] = V + 1;
    bfs(t);
    for(auto &e : graph[s]) {
      if(potential[e.to] < V) {
        graph[e.to][e.rev].cap = e.cap;
        ex[s] -= e.cap;
        ex[e.to] += e.cap;
      }
      e.cap = 0;
    }
    return calc_active(t);
  }

  bool push(int u, int t, edge &e) {
    flow_t f = min(e.cap, ex[u]);
    int v = e.to;
    e.cap -= f, ex[u] -= f;
    graph[v][e.rev].cap += f, ex[v] += f;
    if(ex[v] == f && v != t) act_ver.push(potential[v], v);
    return ex[u] == 0;
  }

  int discharge(int u, int t) {
    for(int &i = cur_edge[u]; i < graph[u].size(); i++) {
      auto &e = graph[u][i];
      if(potential[u] == potential[e.to] + 1 && e.cap > 0) {
        if(push(u, t, e)) return potential[u];
      }
    }
    return relabel(u);
  }

  int global_relabel(int t) {
    bfs(t);
    all_ver.clear(), act_ver.clear();
    return calc_active(t);
  }

  void gap_relabel(const int u) {
    for(int i = potential[u]; i <= height; ++i) {
      for(int id = all_ver.dat[V + i].next; id < V; id = all_ver.dat[id].next) {
        potential[id] = V + 1;
      }
      all_ver.dat[V + i].prev = all_ver.dat[V + i].next = V + i;
    }
  }

  int relabel(const int u) {
    ++relabels;
    int prv = potential[u], cur = V;
    for(int i = 0; i < (int) graph[u].size(); ++i) {
      const edge &e = graph[u][i];
      if(cur > potential[e.to] + 1 && e.cap > 0) {
        cur_edge[u] = i;
        cur = potential[e.to] + 1;
      }
    }
    if(all_ver.more_one(prv)) {
      all_ver.erase(u);
      if((potential[u] = cur) == V) return potential[u] = V + 1, prv;
      act_ver.push(cur, u);
      all_ver.insert(cur, u);
      height = max(height, cur);
    } else {
      gap_relabel(u);
      return height = prv - 1;
    }
    return cur;
  }

  flow_t max_flow(int s, int t) {
    int level = init(s, t);
    while(level >= 0) {
      if(act_ver.empty(level)) {
        --level;
        continue;
      }
      int u = act_ver.top(level);
      act_ver.pop(level);
      level = discharge(u, t);
      if(relabels * 2 >= V) {
        level = global_relabel(t);
        relabels = 0;
      }
    }
    return ex[t];
  }
};

template <template <typename> class C, typename T>
struct ProjectSelectionProblem {
  ProjectSelectionProblem(int n, T TINF) : n(n), TINF(TINF) {}

  void add_diff(int u, int v, T cost) {
    assert(cost >= 0);
    diff_u.emplace_back(u);
    diff_v.emplace_back(v);
    diff_cost.emplace_back(cost);
  }

  void add_same(int u, int v, int group, T cost) {
    assert(cost <= 0);
    cost *= -1;
    res += cost;
    add(n, group ^ 1, cost);
    if (group == 0) {
      add_diff(n, u, TINF);
      add_diff(n, v, TINF);
    } else if (group == 1) {
      add_diff(u, n, TINF);
      add_diff(v, n, TINF);
    }
    ++n;
  }

  void add(int ver, int group, T cost) {
    if (cost < 0) {
      cost *= -1;
      res += cost;
      add(ver, group ^ 1, cost);
    } else {
      if (group == 0) {
        add_diff(ver, -1, cost);
      } else {
        add_diff(-2, ver, cost);
      }
    }
  }

  T solve() {
    C<T> flow(n + 2);
    int sz = diff_u.size();
    REP(i, sz) {
      if (diff_u[i] < 0) diff_u[i] += n + 2;
      if (diff_v[i] < 0) diff_v[i] += n + 2;
      flow.add_edge(diff_u[i], diff_v[i], diff_cost[i]);
    }
    sz = same_u.size();
    REP(i, sz) {
      if (same_u[i] < 0) same_u[i] += n + 2;
      if (same_v[i] < 0) same_v[i] += n + 2;
      flow.add_edge(same_u[i], same_v[i], same_cost[i]);
    }
    return flow.max_flow(n, n + 1) - res;
    // return flow.maximum_flow(n, n + 1, TINF) - res;
  }

private:
  int n;
  T TINF, res = 0;
  vector<int> diff_u, diff_v, same_u, same_v;
  vector<T> diff_cost, same_cost;
};

int main() {
  int h, w; cin >> h >> w;
  vector<vector<int> > g(h, vector<int>(w)); REP(i, h) REP(j, w) cin >> g[i][j];
  vector<int> r(h), c(w);
  REP(i, h) cin >> r[i];
  REP(i, w) cin >> c[i];
  ProjectSelectionProblem<PushRelabel, long long> psp(h + w, LINF);
  REP(i, h) {
    long long cost = -r[i];
    REP(j, w) cost += g[i][j];
    psp.add(i, 1, cost);
  }
  REP(j, w) {
    long long cost = -c[j];
    REP(i, h) cost += g[i][j];
    psp.add(h + j, 1, cost);
  }
  REP(i, h) REP(j, w) psp.add_same(i, h + j, 1, -g[i][j]);
  cout << -psp.solve() << '\n';
  return 0;
}
0