結果

問題 No.957 植林
ユーザー outlineoutline
提出日時 2021-02-28 21:59:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,409 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 150,632 KB
実行使用メモリ 31,764 KB
最終ジャッジ日時 2024-04-12 10:45:57
合計ジャッジ時間 17,513 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,752 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 954 ms
30,132 KB
testcase_04 AC 637 ms
27,708 KB
testcase_05 AC 1,102 ms
30,904 KB
testcase_06 AC 445 ms
31,684 KB
testcase_07 AC 1,284 ms
28,164 KB
testcase_08 AC 485 ms
30,196 KB
testcase_09 AC 478 ms
29,816 KB
testcase_10 AC 575 ms
30,664 KB
testcase_11 AC 543 ms
30,980 KB
testcase_12 AC 528 ms
29,192 KB
testcase_13 AC 241 ms
26,980 KB
testcase_14 AC 391 ms
31,764 KB
testcase_15 AC 359 ms
29,624 KB
testcase_16 AC 259 ms
27,008 KB
testcase_17 AC 290 ms
27,456 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
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 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

/**
 * @brief Dinic-Capacity-Scaling(最大流)
 * @docs docs/dinic-capacity-scaling.md
 */
template< typename flow_t >
struct DinicCapacityScaling {
  static_assert(is_integral< flow_t >::value, "template parameter flow_t must be integral type");

  const flow_t INF;

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

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

  explicit DinicCapacityScaling(int V) : INF(numeric_limits< flow_t >::max()), graph(V), max_cap(0) {}

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

  bool build_augment_path(int s, int t, const flow_t &base) {
    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 >= base && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return min_cost[t] != -1;
  }

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

  flow_t max_flow(int s, int t) {
    if(max_cap == flow_t(0)) return flow_t(0);
    flow_t flow = 0;
    for(int i = 63 - __builtin_clzll(max_cap); i >= 0; i--) {
      flow_t now = flow_t(1) << i;
      while(build_augment_path(s, t, now)) {
        iter.assign(graph.size(), 0);
        flow += find_augment_path(s, t, now, INF);
      }
    }
    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(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int H, W;
    cin >> H >> W;
    vector<vector<ll>> G(H, vector<ll>(W));
    int s = H * W + H + W, t = s + 1, V = s + 2;
    DinicCapacityScaling<ll> g(V);
    ll ans = 0;
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            cin >> G[i][j];
            int v = i * W + j;
            g.add_edge(s, v, 0);
            g.add_edge(v, t, G[i][j]);
            int w = H * W + i, x = H * W + H + j;
            g.add_edge(w, v, INF);
            g.add_edge(x, v, INF);
        }
    }
    vector<ll> R(H), C(W);
    for(int i = 0; i < H; ++i){
        cin >> R[i];
        ans += R[i];
        int v = H * W + i;
        g.add_edge(s, v, R[i]);
        g.add_edge(v, t, 0);
    }
    for(int i = 0; i < W; ++i){
        cin >> C[i];
        ans += C[i];
        int v = H * W + H + i;
        g.add_edge(s, v, C[i]);
        g.add_edge(v, t, 0);
    }
    
    ans -= g.max_flow(s, t);
    cout << ans << endl;

    return 0;
}
0