結果

問題 No.957 植林
ユーザー 👑 emthrmemthrm
提出日時 2019-12-20 01:53:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,597 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 138,392 KB
実行使用メモリ 21,616 KB
最終ジャッジ日時 2023-09-21 08:24:16
合計ジャッジ時間 7,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 113 ms
19,740 KB
testcase_04 AC 113 ms
18,520 KB
testcase_05 AC 97 ms
20,420 KB
testcase_06 AC 149 ms
21,288 KB
testcase_07 AC 123 ms
19,276 KB
testcase_08 AC 49 ms
19,984 KB
testcase_09 AC 51 ms
19,988 KB
testcase_10 AC 57 ms
20,848 KB
testcase_11 AC 55 ms
20,032 KB
testcase_12 AC 56 ms
20,028 KB
testcase_13 AC 39 ms
18,252 KB
testcase_14 AC 49 ms
21,616 KB
testcase_15 AC 44 ms
19,728 KB
testcase_16 AC 38 ms
18,144 KB
testcase_17 AC 39 ms
18,936 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 <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;
/*-------------------------------------------------*/
template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {}
  };

  vector<vector<Edge> > graph;

  Dinic(int n) : graph(n), level(n), itr(n) {}

  void add_edge(int src, int dst, T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(int s, int t, T limit) {
    T res = 0;
    while (true) {
      bfs(s);
      if (level[t] == -1) return res;
      fill(ALL(itr), 0);
      T tmp;
      while ((tmp = dfs(s, t, limit)) > 0) res += tmp;
    }
  }

private:
  vector<int> level, itr;

  void bfs(int s) {
    fill(ALL(level), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int ver = que.front(); que.pop();
      for (Edge e : graph[ver]) if (level[e.dst] == -1 && e.cap > 0) {
        level[e.dst] = level[ver] + 1;
        que.emplace(e.dst);
      }
    }
  }

  T dfs(int ver, int t, T flow) {
    if (ver == t) return flow;
    for (; itr[ver] < graph[ver].size(); ++itr[ver]) {
      Edge &e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        T tmp = dfs(e.dst, t, min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

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.maximum_flow(n, n + 1, TINF) - res;
  }

private:
  int n;
  T TINF, res = 0;
  vector<int> diff_u, diff_v, diff_cost, same_u, same_v, 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<Dinic, 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