結果

問題 No.957 植林
ユーザー bokusunny
提出日時 2022-01-19 16:33:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 486 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 3,148 ms
コンパイル使用メモリ 210,580 KB
最終ジャッジ日時 2025-01-27 13:06:49
ジャッジサーバー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;
  vector Cost(H, vector<long long>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> Cost[i][j];
    }
  }
  vector<long long> R(H), C(W);
  for (int i = 0; i < H; i++) cin >> R[i];
  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;
  const int g = H + W + 1;

  for (int h = 0; h < H; h++) {
    int cur = h;
    long long sum = 0;
    for (int w = 0; w < W; w++) sum += Cost[h][w];
    G.add_edge(s, cur, sum);
    G.add_edge(cur, g, R[h]);
  }
  for (int w = 0; w < W; w++) {
    int cur = H + w;
    G.add_edge(s, cur, 0);
    G.add_edge(cur, g, C[w]);
  }
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      int from = h;
      int to = H + w;
      G.add_edge(from, to, Cost[h][w]);
    }
  }

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

int main() {
  bokusunny;
  solve();

  return 0;
}
0