結果

問題 No.957 植林
ユーザー risujirohrisujiroh
提出日時 2019-12-19 22:08:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,273 bytes
コンパイル時間 1,809 ms
コンパイル使用メモリ 176,572 KB
実行使用メモリ 22,184 KB
最終ジャッジ日時 2023-09-21 07:28:16
合計ジャッジ時間 6,742 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 86 ms
17,412 KB
testcase_04 AC 80 ms
16,528 KB
testcase_05 AC 82 ms
17,768 KB
testcase_06 AC 89 ms
18,756 KB
testcase_07 AC 87 ms
16,936 KB
testcase_08 AC 56 ms
17,736 KB
testcase_09 AC 53 ms
17,860 KB
testcase_10 AC 64 ms
18,452 KB
testcase_11 AC 57 ms
17,924 KB
testcase_12 AC 58 ms
17,464 KB
testcase_13 AC 44 ms
15,144 KB
testcase_14 AC 58 ms
19,052 KB
testcase_15 AC 55 ms
17,548 KB
testcase_16 AC 48 ms
15,388 KB
testcase_17 AC 50 ms
16,892 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;

template<class T> struct Dinic {
  struct Edge { int to, rev; T cap; };
  const T inf = numeric_limits<T>::max();
  const int n;
  vector< vector<Edge> > g;
  vector<int> dist, i;
  Dinic(int _n) : n(_n), g(n), dist(n), i(n) {}
  void add_edge(int from, int to, T cap) {
    assert(from != to);
    assert(cap >= 0);
    if (!cap) return;
    g[from].emplace_back(Edge{to, (int)g[to].size(), cap});
    g[to].emplace_back(Edge{from, (int)g[from].size() - 1, 0});
  }
  void bfs(int s) {
    fill(begin(dist), end(dist), -1);
    queue<int> que;
    dist[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front(); que.pop();
      for (const auto& e : g[v]) {
        if (dist[e.to] != -1 or !e.cap) continue;
        dist[e.to] = dist[v] + 1;
        que.push(e.to);
      }
    }
  }
  T dfs(int v, int s, T f) {
    if (v == s) return f;
    for (; i[v] < (int)g[v].size(); ++i[v]) {
      Edge& e = g[v][i[v]];
      if (dist[e.to] >= dist[v] or !g[e.to][e.rev].cap) continue;
      T d = dfs(e.to, s, min(f, g[e.to][e.rev].cap));
      if (d > 0) {
        g[e.to][e.rev].cap -= d;
        e.cap += d;
        return d;
      }
    }
    return 0;
  }
  T max_flow(int s, int t) {
    assert(s != t);
    T res = 0;
    while (true) {
      bfs(s);
      if (dist[t] == -1) return res;
      fill(begin(i), end(i), 0);
      while (true) {
        T f = dfs(t, s, inf);
        if (!f) break;
        res += f;
      }
    }
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int s = h * w + h + w, t = s + 1;
  Dinic<long long> g(t + 1);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      int a;
      cin >> a;
      g.add_edge(i * w + j, t, a);
    }
  }
  long long res = 0;
  for (int i = 0; i < h; ++i) {
    int a;
    cin >> a;
    res += a;
    g.add_edge(s, h * w + i, a);
    for (int j = 0; j < w; ++j) {
      g.add_edge(h * w + i, i * w + j, g.inf);
    }
  }
  for (int j = 0; j < w; ++j) {
    int a;
    cin >> a;
    res += a;
    g.add_edge(s, h * w + h + j, a);
    for (int i = 0; i < h; ++i) {
      g.add_edge(h * w + h + j, i * w + j, g.inf);
    }
  }
  res -= g.max_flow(s, t);
  cout << res << '\n';
}
0