cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: グラフ/フロー/最大流/submodular quadratic pseudo-Boolean optimisation
(test/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.test.cpp)

Depends on

Code

/*
 * @title グラフ/フロー/最大流/submodular quadratic pseudo-Boolean optimisation
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2903
 */

#include <iostream>
#include <string>
#include <vector>

#include "emthrm/graph/flow/maximum_flow/dinic.hpp"
#include "emthrm/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.hpp"

int main() {
  int r, c;
  std::cin >> r >> c;
  std::vector<std::string> s(r);
  for (int i = 0; i < r; ++i) {
    std::cin >> s[i];
  }
  std::vector<std::vector<int>> id(r, std::vector<int>(c, -1));
  int n = 0;
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (s[i][j] == '#') id[i][j] = n++;
    }
  }
  emthrm::SubmodularQPBO<emthrm::Dinic, int> submodular_qpbo(n);
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (id[i][j] == -1) continue;
      if (i + 1 < r && id[i + 1][j] != -1) {
        submodular_qpbo.add_eq(id[i][j], id[i + 1][j], 0, -1);
      }
      if (j + 1 < c && id[i][j + 1] != -1) {
        submodular_qpbo.add_eq(id[i][j], id[i][j + 1], 1, -1);
      }
    }
  }
  std::cout << n + submodular_qpbo.solve() << '\n';
  return 0;
}
#line 1 "test/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.test.cpp"
/*
 * @title グラフ/フロー/最大流/submodular quadratic pseudo-Boolean optimisation
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2903
 */

#include <iostream>
#include <string>
#include <vector>

#line 1 "include/emthrm/graph/flow/maximum_flow/dinic.hpp"



#include <algorithm>
#include <limits>
#include <queue>
#include <utility>
#line 9 "include/emthrm/graph/flow/maximum_flow/dinic.hpp"

namespace emthrm {

template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    explicit Edge(const int dst, const T cap, const int rev)
        : dst(dst), rev(rev), cap(cap) {}
  };

  std::vector<std::vector<Edge>> graph;

  explicit Dinic(const int n) : graph(n), level(n), itr(n) {}

  void add_edge(const int src, const int dst, const T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(const int s, const int t,
                 T limit = std::numeric_limits<T>::max()) {
    T res = 0;
    while (limit > 0) {
      std::fill(level.begin(), level.end(), -1);
      level[s] = 0;
      std::queue<int> que;
      que.emplace(s);
      while (!que.empty()) {
        const int ver = que.front();
        que.pop();
        for (const Edge& e : graph[ver]) {
          if (level[e.dst] == -1 && e.cap > 0) {
            level[e.dst] = level[ver] + 1;
            que.emplace(e.dst);
          }
        }
      }
      if (level[t] == -1) break;
      std::fill(itr.begin(), itr.end(), 0);
      while (limit > 0) {
        const T f = dfs(s, t, limit);
        if (f == 0) break;
        limit -= f;
        res += f;
      }
    }
    return res;
  }

 private:
  std::vector<int> level, itr;

  T dfs(const int ver, const int t, const T flow) {
    if (ver == t) return flow;
    for (; std::cmp_less(itr[ver], graph[ver].size()); ++itr[ver]) {
      Edge& e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        const T tmp = dfs(e.dst, t, std::min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

}  // namespace emthrm


#line 1 "include/emthrm/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.hpp"



#include <cassert>
#line 7 "include/emthrm/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.hpp"

#line 1 "include/emthrm/graph/flow/maximum_flow/maximum_flow.hpp"
/**
 * @title 最大流コンセプト
 */

#ifndef EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_
#define EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_

#include <concepts>
#line 10 "include/emthrm/graph/flow/maximum_flow/maximum_flow.hpp"

namespace emthrm {

template <template <typename> class C, typename T>
concept MaximumFlow = requires (C<T> mf) {
  {mf.add_edge(std::declval<int>(), std::declval<int>(), std::declval<T>())}
      -> std::same_as<void>;
  {mf.maximum_flow(std::declval<int>(), std::declval<int>())}
      -> std::same_as<T>;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_
#line 9 "include/emthrm/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.hpp"

namespace emthrm {

template <template <typename> class C, typename T>
requires MaximumFlow<C, T>
struct SubmodularQPBO {
  explicit SubmodularQPBO(const int n)
      : inf(std::numeric_limits<T>::max()), n(n), res(0) {}

  void add_neq(const int u, const int v, const T cost) {
    assert(cost >= 0);
    us.emplace_back(u);
    vs.emplace_back(v);
    costs.emplace_back(cost);
  }

  void add(const int v, bool group, T cost) {
    if (cost < 0) {
      cost = -cost;
      res += cost;
      group = !group;
    }
    if (group) {
      add_neq(-2, v, cost);  // -2 represents S.
    } else {
      add_neq(v, -1, cost);  // -1 represents T.
    }
  }

  void add_or(const std::vector<int>& v, const bool group, const T cost) {
    assert(cost >= 0);
    add(n, group, cost);
    if (group) {
      for (const int e : v) add_neq(n, e, inf);
    } else {
      for (const int e : v) add_neq(e, n, inf);
    }
    ++n;
  }

  void add_or(const int u, const int v, const bool group, const T cost) {
    add_or({u, v}, group, cost);
  }

  void add_eq(const std::vector<int>& v, const bool group, T cost) {
    assert(cost <= 0);
    cost = -cost;
    res += cost;
    add_or(v, !group, cost);
  }

  void add_eq(const int u, const int v, const bool group, const T cost) {
    add_eq({u, v}, group, cost);
  }

  T solve() {
    C<T> mf(n + 2);
    const int neq_size = costs.size();
    for (int i = 0; i < neq_size; ++i) {
      mf.add_edge(us[i] < 0 ? us[i] + n + 2 : us[i],
                  vs[i] < 0 ? vs[i] + n + 2 : vs[i], costs[i]);
    }
    return mf.maximum_flow(n, n + 1, inf) - res;
  }

 private:
  const T inf;
  int n;
  T res;
  std::vector<int> us, vs;
  std::vector<T> costs;
};

}  // namespace emthrm


#line 13 "test/graph/flow/maximum_flow/submodular_quadratic_pseudo-boolean_optimisation.test.cpp"

int main() {
  int r, c;
  std::cin >> r >> c;
  std::vector<std::string> s(r);
  for (int i = 0; i < r; ++i) {
    std::cin >> s[i];
  }
  std::vector<std::vector<int>> id(r, std::vector<int>(c, -1));
  int n = 0;
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (s[i][j] == '#') id[i][j] = n++;
    }
  }
  emthrm::SubmodularQPBO<emthrm::Dinic, int> submodular_qpbo(n);
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (id[i][j] == -1) continue;
      if (i + 1 < r && id[i + 1][j] != -1) {
        submodular_qpbo.add_eq(id[i][j], id[i + 1][j], 0, -1);
      }
      if (j + 1 < c && id[i][j + 1] != -1) {
        submodular_qpbo.add_eq(id[i][j], id[i][j + 1], 1, -1);
      }
    }
  }
  std::cout << n + submodular_qpbo.solve() << '\n';
  return 0;
}
Back to top page