comp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub luzhiled1333/comp-library

:heavy_check_mark: test/aoj/grl_6_a.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/GRL_6_A

#include "src/cpp-template/header/input.hpp"
#include "src/cpp-template/header/int-alias.hpp"
#include "src/cpp-template/header/rep.hpp"
#include "src/cpp-template/header/size-alias.hpp"
#include "src/graph/flow/max-flow.hpp"

#include <iostream>

namespace luz {

  void main_() {
    usize n, m;
    std::cin >> n >> m;

    MaxFlowGraph< u32 > g(n);
    for ([[maybe_unused]] usize _: rep(0, m)) {
      usize u, v;
      u32 c;
      std::cin >> u >> v >> c;
      g.add_directed_edge(u, v, c);
    }
    std::cout << g.max_flow(0, n - 1) << std::endl;
  }

} // namespace luz

int main() {
  luz::main_();
}
#line 1 "test/aoj/grl_6_a.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/GRL_6_A

#line 2 "src/cpp-template/header/input.hpp"

#line 2 "src/cpp-template/header/int-alias.hpp"

#include <cstdint>

namespace luz {

  using i32  = std::int32_t;
  using i64  = std::int64_t;
  using i128 = __int128_t;

  using u32  = std::uint32_t;
  using u64  = std::uint64_t;
  using u128 = __uint128_t;

} // namespace luz
#line 4 "src/cpp-template/header/input.hpp"

#include <iostream>

namespace luz {

  template < typename T = i64 >
  T input() {
    T tmp;
    std::cin >> tmp;
    return tmp;
  }

} // namespace luz
#line 2 "src/cpp-template/header/rep.hpp"

#line 2 "src/cpp-template/header/size-alias.hpp"

#include <cstddef>

namespace luz {

  using isize = std::ptrdiff_t;
  using usize = std::size_t;

} // namespace luz
#line 4 "src/cpp-template/header/rep.hpp"

#include <algorithm>

namespace luz {

  struct rep {
    struct itr {
      usize i;
      constexpr itr(const usize i) noexcept: i(i) {}
      void operator++() noexcept {
        ++i;
      }
      constexpr usize operator*() const noexcept {
        return i;
      }
      constexpr bool operator!=(const itr x) const noexcept {
        return i != x.i;
      }
    };
    const itr f, l;
    constexpr rep(const usize f, const usize l) noexcept
        : f(std::min(f, l)),
          l(l) {}
    constexpr auto begin() const noexcept {
      return f;
    }
    constexpr auto end() const noexcept {
      return l;
    }
  };

  struct rrep {
    struct itr {
      usize i;
      constexpr itr(const usize i) noexcept: i(i) {}
      void operator++() noexcept {
        --i;
      }
      constexpr usize operator*() const noexcept {
        return i;
      }
      constexpr bool operator!=(const itr x) const noexcept {
        return i != x.i;
      }
    };
    const itr f, l;
    constexpr rrep(const usize f, const usize l) noexcept
        : f(l - 1),
          l(std::min(f, l) - 1) {}
    constexpr auto begin() const noexcept {
      return f;
    }
    constexpr auto end() const noexcept {
      return l;
    }
  };

} // namespace luz
#line 2 "src/graph/flow/max-flow.hpp"

#line 5 "src/graph/flow/max-flow.hpp"

#line 7 "src/graph/flow/max-flow.hpp"
#include <cassert>
#include <limits>
#include <queue>
#include <vector>

namespace luz {

  template < typename cap_type >
  class MaxFlowGraph {
    static constexpr cap_type INF =
        std::numeric_limits< cap_type >::max();

    struct Edge {
      usize to;
      cap_type cap;
      usize rev;
      Edge() = default;
      Edge(usize to, cap_type cap, usize rev)
          : to(to),
            cap(cap),
            rev(rev) {}
    };

    usize g_size;
    std::vector< i32 > min_cost;
    std::vector< usize > iter;
    std::vector< std::vector< Edge > > graph;

    bool build_augment_path(usize s, usize t) {
      min_cost.assign(g_size, -1);
      std::queue< usize > que;
      que.push(s);
      min_cost[s] = 0;
      while (not que.empty() and min_cost[t] == -1) {
        usize v = que.front();
        que.pop();
        for (const auto &e: graph[v]) {
          if (e.cap > 0 and min_cost[e.to] == -1) {
            min_cost[e.to] = min_cost[v] + 1;
            que.push(e.to);
          }
        }
      }
      return min_cost[t] != -1;
    }

    cap_type find_augment_path(usize v, usize t,
                               cap_type flow_limit) {
      if (v == t) return flow_limit;
      for (usize &i = iter[v]; i < graph[v].size(); i++) {
        Edge &e = graph[v][i];
        if (e.cap > 0 and min_cost[v] + 1 == min_cost[e.to]) {
          cap_type d =
              find_augment_path(e.to, t, std::min(flow_limit, e.cap));
          if (d > 0) {
            e.cap -= d;
            graph[e.to][e.rev].cap += d;
            return d;
          }
        }
      }
      return 0;
    }

   public:
    MaxFlowGraph() = default;

    explicit MaxFlowGraph(usize n): g_size(n), graph(n) {}

    void add_directed_edge(usize from, usize to, cap_type cap) {
      assert(from < g_size);
      assert(to < g_size);
      assert(from != to);
      graph[from].emplace_back(to, cap, graph[to].size());
      graph[to].emplace_back(from, 0, graph[from].size() - 1);
    }

    inline cap_type inf() const {
      return INF;
    }

    cap_type max_flow(usize s, usize t) {
      return max_flow(s, t, inf());
    }

    cap_type max_flow(usize s, usize t, cap_type flow_limit) {
      assert(s < g_size);
      assert(t < g_size);
      assert(s != t);
      cap_type flow = 0, add = 0;
      while (build_augment_path(s, t) and flow < flow_limit) {
        iter.assign(g_size, 0);
        do {
          add = find_augment_path(s, t, flow_limit - add);
          flow += add;
        } while (add > 0);
      }
      return flow;
    }
  };

} // namespace luz
#line 8 "test/aoj/grl_6_a.test.cpp"

#line 10 "test/aoj/grl_6_a.test.cpp"

namespace luz {

  void main_() {
    usize n, m;
    std::cin >> n >> m;

    MaxFlowGraph< u32 > g(n);
    for ([[maybe_unused]] usize _: rep(0, m)) {
      usize u, v;
      u32 c;
      std::cin >> u >> v >> c;
      g.add_directed_edge(u, v, c);
    }
    std::cout << g.max_flow(0, n - 1) << std::endl;
  }

} // namespace luz

int main() {
  luz::main_();
}
Back to top page