結果

問題 No.957 植林
ユーザー bokusunny
提出日時 2022-05-01 23:48:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 1,080 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 210,232 KB
最終ジャッジ日時 2025-01-29 01:26:37
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

#include <atcoder/maxflow>
using namespace atcoder;

void solve() {
  int H, W;
  cin >> H >> W;
  using Grid = vector<vector<int>>;
  Grid grid(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> grid[i][j];
    }
  }
  vector<int> R(H);
  for (int i = 0; i < H; i++) cin >> R[i];
  vector<int> C(W);
  for (int i = 0; i < W; i++) cin >> C[i];

  using Cap = long long;
  mf_graph<Cap> G(H + W + 2);
  const int s = H + W, t = H + W + 1;
  for (int h = 0; h < H; h++) {
    G.add_edge(s, h, R[h]);
    long long tot = 0;
    for (auto &g : grid[h]) tot += g;
    G.add_edge(h, t, tot);
  }
  for (int w = 0; w < W; w++) {
    G.add_edge(s, H + w, C[w]);
    for (int h = 0; h < H; h++) {
      G.add_edge(H + w, h, grid[h][w]);
    }
  }

  auto ans = accumulate(R.begin(), R.end(), 0LL) + accumulate(C.begin(), C.end(), 0LL) - G.flow(s, t);
  cout << ans << endl;
}

int main() {
  bokusunny;
  solve();

  return 0;
}
0