結果

問題 No.957 植林
ユーザー ei1333333ei1333333
提出日時 2019-12-03 03:27:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,809 bytes
コンパイル時間 2,502 ms
コンパイル使用メモリ 216,796 KB
実行使用メモリ 26,716 KB
最終ジャッジ日時 2023-09-21 07:14:31
合計ジャッジ時間 8,157 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,764 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 137 ms
22,880 KB
testcase_04 AC 113 ms
21,252 KB
testcase_05 AC 128 ms
22,668 KB
testcase_06 AC 136 ms
23,408 KB
testcase_07 AC 148 ms
20,996 KB
testcase_08 AC 83 ms
22,448 KB
testcase_09 AC 78 ms
22,556 KB
testcase_10 AC 92 ms
23,068 KB
testcase_11 AC 95 ms
22,104 KB
testcase_12 AC 88 ms
22,256 KB
testcase_13 AC 65 ms
19,772 KB
testcase_14 AC 85 ms
23,976 KB
testcase_15 AC 79 ms
23,040 KB
testcase_16 AC 67 ms
19,668 KB
testcase_17 AC 69 ms
20,780 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;

using int64 = long long;

template< typename flow_t >
struct Dinic {
  const flow_t INF;

  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
  };

  vector< vector< edge > > graph;
  vector< int > min_cost, iter;

  Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}

  void add_edge(int from, int to, flow_t cap) {
    graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true});
  }

  bool bfs(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue< int > que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty() && min_cost[t] == -1) {
      int p = que.front();
      que.pop();
      for(auto &e : graph[p]) {
        if(e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return min_cost[t] != -1;
  }

  flow_t dfs(int idx, const int t, flow_t flow) {
    if(idx == t) return flow;
    for(int &i = iter[idx]; i < graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        flow_t d = dfs(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  flow_t max_flow(int s, int t) {
    flow_t flow = 0;
    while(bfs(s, t)) {
      iter.assign(graph.size(), 0);
      flow_t f = 0;
      while((f = dfs(s, t, INF)) > 0) flow += f;
    }
    return flow;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }
};


int main() {
  int H, W;
  cin >> H >> W;
  vector< vector< int64 > > G(H, vector< int64 >(W));
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) cin >> G[i][j];
  }
  vector< int64 > R(H), C(W);
  for(int i = 0; i < H; i++) cin >> R[i];
  for(int i = 0; i < W; i++) cin >> C[i];

  int64 sum = accumulate(begin(R), end(R), 0LL) + accumulate(begin(C), end(C), 0LL);

  Dinic< int64 > flow(H * W + H + W + 2);
  int S = H * W + H + W;
  int T = S + 1;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      flow.add_edge(i * W + j, S, G[i][j]);
      flow.add_edge(H * W + i, i * W + j, flow.INF);
      flow.add_edge(H * W + H + j, i * W + j, flow.INF);
    }
  }
  for(int i = 0; i < H; i++) {
    flow.add_edge(T, H * W + i, R[i]);
  }
  for(int i = 0; i < W; i++) {
    flow.add_edge(T, H * W + H + i, C[i]);
  }

  cout << sum - flow.max_flow(T, S) << endl;
}

0