結果

問題 No.957 植林
ユーザー pekempeypekempey
提出日時 2019-12-20 03:31:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 2,332 bytes
コンパイル時間 1,958 ms
コンパイル使用メモリ 184,104 KB
実行使用メモリ 15,168 KB
最終ジャッジ日時 2023-09-21 10:59:13
合計ジャッジ時間 15,377 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 74 ms
13,868 KB
testcase_04 AC 67 ms
15,168 KB
testcase_05 AC 72 ms
14,836 KB
testcase_06 AC 79 ms
14,464 KB
testcase_07 AC 79 ms
14,240 KB
testcase_08 AC 45 ms
14,072 KB
testcase_09 AC 44 ms
13,960 KB
testcase_10 AC 50 ms
14,440 KB
testcase_11 AC 48 ms
14,040 KB
testcase_12 AC 47 ms
13,988 KB
testcase_13 AC 37 ms
14,152 KB
testcase_14 AC 46 ms
13,788 KB
testcase_15 AC 43 ms
14,968 KB
testcase_16 AC 38 ms
14,820 KB
testcase_17 AC 39 ms
13,732 KB
testcase_18 AC 391 ms
13,528 KB
testcase_19 AC 423 ms
15,064 KB
testcase_20 AC 425 ms
13,668 KB
testcase_21 AC 449 ms
13,500 KB
testcase_22 AC 448 ms
14,228 KB
testcase_23 AC 461 ms
13,488 KB
testcase_24 AC 486 ms
14,020 KB
testcase_25 AC 512 ms
13,784 KB
testcase_26 AC 501 ms
14,256 KB
testcase_27 AC 506 ms
14,432 KB
testcase_28 AC 501 ms
13,776 KB
testcase_29 AC 505 ms
14,808 KB
testcase_30 AC 501 ms
14,120 KB
testcase_31 AC 375 ms
13,660 KB
testcase_32 AC 385 ms
13,748 KB
testcase_33 AC 401 ms
13,752 KB
testcase_34 AC 427 ms
13,572 KB
testcase_35 AC 447 ms
14,500 KB
testcase_36 AC 461 ms
13,884 KB
testcase_37 AC 470 ms
13,948 KB
testcase_38 AC 509 ms
14,716 KB
testcase_39 AC 494 ms
13,768 KB
testcase_40 AC 495 ms
13,728 KB
testcase_41 AC 25 ms
14,652 KB
testcase_42 AC 24 ms
14,188 KB
testcase_43 AC 50 ms
13,960 KB
testcase_44 AC 51 ms
13,512 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

struct Dinic {
  struct edge { int v; long long c; };
  vector<edge> es;
  vector<vector<int>> g;
  vector<int> dist;
  vector<int> index;
 
  Dinic(int n) : g(n), dist(n), index(n) {}
 
  void add(int u, int v, long long c) {
    g[u].push_back(es.size());
    g[v].push_back(es.size() + 1);
    es.push_back(edge{v, c});
    es.push_back(edge{u, 0});
  }
 
  long long calc(int s, int t) {
    long long ans = 0;
    while (bfs(s, t)) {
      for (;;) {
        long long d = dfs(s, t, 1e18);
        if (d == 0) break;
        ans += d;
      }
    }
    return ans;
  }
 
  bool bfs(int s, int t) {
    fill(dist.begin(), dist.end(), -1);
    fill(index.begin(), index.end(), 0);
    queue<int> q;
    q.push(t);
    dist[t] = 0;
    while (!q.empty()) {
      int u = q.front(); q.pop();
      for (int i : g[u]) {
        int v = es[i].v;
        if (es[i ^ 1].c > 0 && dist[v] == -1) {
          dist[v] = dist[u] + 1;
          q.push(v);
        }
      }
    }
    return dist[s] != -1;
  }
 
  long long dfs(int u, int t, long long f) {
    if (u == t) return f;
    for (int &it = index[u]; it < g[u].size(); it++) {
      int i = g[u][it];
      int v = es[i].v;
      if (es[i].c > 0 && dist[v] < dist[u]) {
        long long d = dfs(v, t, min(f, es[i].c));
        if (d > 0) {
          es[i ^ 0].c -= d;
          es[i ^ 1].c += d;
          return d;
        }
      }
    }
    return 0;
  }
};

int main() {
  int H, W; cin >> H >> W;
  vector<vector<ll>> G(H, vector<ll>(W));
  rep(i, H) rep(j, W) cin >> G[i][j];
  vector<ll> R(H), C(W);
  rep(i, H) cin >> R[i];
  rep(j, W) cin >> C[j];
  vector<ll> row(H), col(W);
  rep(i, H) rep(j, W) row[i] += G[i][j];
  rep(j, W) rep(i, H) col[j] += G[i][j];
  Dinic mf(H + W + 2);
  ll total = accumulate(range(R), 0LL) + accumulate(range(C), 0LL);
  int s = H + W;
  int t = s + 1;
  rep(i, H) mf.add(i, t, row[i]);
  rep(j, W) mf.add(j + H, t, col[j]);
  rep(i, H) mf.add(s, i, 2*R[i]);
  rep(j, W) mf.add(s, j + H, 2*C[j]);
  rep(i, H) rep(j, W) {
    mf.add(i, j + H, G[i][j]);
    mf.add(j + H, i, G[i][j]);
  }
  cout << total - mf.calc(s, t)/2 << endl;
}
0