CompetitiveProgrammingCpp

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

View the Project on GitHub

:heavy_check_mark: Test/Graph/Normal/BFS.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1473"

#include <iostream>

#include <vector>


// begin:tag includes

#include "./../../../Library/Algorithms/BinarySearch.hpp"

#include "./../../../Library/Graph/Graph.hpp"

#include "./../../../Library/Graph/Normal/BFS.hpp"

// end:tag includes


signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(0);

  int n, m;
  std::cin >> n >> m;
  auto graph_all = mtd::Graph(n);
  for (int i = 0; i < m; ++i) {
    int s, t, d;
    std::cin >> s >> t >> d;
    graph_all.addEdge(s - 1, t - 1, d);
  }

  auto solve = [&](int w) {
    auto graph = mtd::Graph(n);
    for (const auto& [s, t, d] : graph_all.getEdges()) {
      if (w <= d) { graph.addArc(s, t); }
    }

    std::vector<int> dv(n);
    bfs(graph, 0, [&](auto f, auto t, auto) { dv[t] = dv[f] + 1; });
    return dv[n - 1];
  };

  auto w_max = mtd::binarySearch(0, static_cast<int>(1e9) + 1, [&](int w) {
    auto d = solve(w);
    return d > 0;
  });

  auto ans = solve(w_max);
  std::cout << w_max << " " << ans << std::endl;
}
#line 1 "Test/Graph/Normal/BFS.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1473"

#include <iostream>

#include <vector>


// begin:tag includes

#line 2 "Library/Algorithms/BinarySearch.hpp"

#include <concepts>

#include <numeric>

#include <ranges>

#include <type_traits>


namespace mtd {

  template <class Lambda>
  auto binarySearch(double ok, double ng, int rep, const Lambda& is_ok) {
    for ([[maybe_unused]] auto _ : std::views::iota(0, rep)) {
      double mid = (ok + ng) / 2.0;
      (is_ok(mid) ? ok : ng) = mid;
    }
    return ok;
  }

  template <class Lambda, std::integral T1, std::integral T2>
  auto binarySearch(T1 ok_, T2 ng_, const Lambda& is_ok) {
    using T = std::common_type_t<T1, T2>;
    T ok = ok_, ng = ng_;
    while (std::abs(ok - ng) > 1) {
      T mid = (ok + ng) >> 1;
      (is_ok(mid) ? ok : ng) = mid;
    }
    return ok;
  }

}  // namespace mtd

#line 2 "Library/Graph/Graph.hpp"
#include <deque>

#line 5 "Library/Graph/Graph.hpp"
#include <tuple>

#line 7 "Library/Graph/Graph.hpp"

namespace mtd {
  template <class Node = long long, class Cost = long long>
  class Graph {
    using Edge = std::pair<Node, Cost>;
    using Edges = std::vector<Edge>;

    const int m_n;
    std::vector<Edges> m_graph;

  public:
    Graph(int n) : m_n(n), m_graph(n) {}
    Graph(const std::vector<Edges>& edges)
        : m_n(edges.size()), m_graph(edges) {}
    Graph(int n, const std::vector<std::tuple<Node, Node>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v);
        } else {
          addEdge(u, v);
        }
      }
    }
    Graph(int n, const std::vector<std::tuple<Node, Node, Cost>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v, c] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v, c);
        } else {
          addEdge(u, v, c);
        }
      }
    }

    auto addEdge(const Node& f, const Node& t, const Cost& c = 1) {
      addArc(f, t, c);
      addArc(t, f, c);
    }
    auto addArc(const Node& f, const Node& t, const Cost& c = 1) {
      m_graph[f].emplace_back(t, c);
    }
    auto getEdges(const Node& from) const {
      class EdgesRange {
        const typename Edges::const_iterator b, e;

      public:
        EdgesRange(const Edges& edges) : b(edges.begin()), e(edges.end()) {}
        auto begin() const { return b; }
        auto end() const { return e; }
      };
      return EdgesRange(m_graph[from]);
    }
    auto getEdges() const {
      std::deque<std::tuple<Node, Node, Cost>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, c] : getEdges(from)) {
          edges.emplace_back(from, to, c);
        }
      }
      return edges;
    }
    auto getEdgesExcludeCost() const {
      std::deque<std::pair<Node, Node>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, _] : getEdges(from)) {
          edges.emplace_back(from, to);
        }
      }
      return edges;
    }
    auto reverse() const {
      auto rev = Graph<Node, Cost>(m_n);
      for (const auto& [from, to, c] : getEdges()) { rev.addArc(to, from, c); }
      return rev;
    }
    auto size() const { return m_n; };
    auto debug(bool directed = false) const {
      for (const auto& [f, t, c] : getEdges()) {
        if (f < t || directed) {
          std::cout << f << " -> " << t << ": " << c << std::endl;
        }
      }
    }
  };
}  // namespace mtd

#line 2 "Library/Graph/Normal/BFS.hpp"

#line 4 "Library/Graph/Normal/BFS.hpp"
#include <queue>

#line 6 "Library/Graph/Normal/BFS.hpp"

#line 8 "Library/Graph/Normal/BFS.hpp"

namespace mtd {
  template <class Node, class Cost, class Lambda,
            std::convertible_to<Node> _Node>
  auto bfs(const Graph<Node, Cost>& graph, const _Node& root,
           const Lambda& lambda) {
    auto n = graph.size();
    std::vector<bool> used(n);
    used[root] = true;
    std::queue<Node> q;
    q.emplace(root);
    while (!q.empty()) {
      auto from = q.front();
      q.pop();
      for (const auto& [to, cost] : graph.getEdges(from)) {
        if (used[to]) { continue; }
        q.emplace(to);
        used[to] = true;
        lambda(from, to, cost);
      }
    }
  }
}  // namespace mtd

#line 10 "Test/Graph/Normal/BFS.test.cpp"
// end:tag includes


signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(0);

  int n, m;
  std::cin >> n >> m;
  auto graph_all = mtd::Graph(n);
  for (int i = 0; i < m; ++i) {
    int s, t, d;
    std::cin >> s >> t >> d;
    graph_all.addEdge(s - 1, t - 1, d);
  }

  auto solve = [&](int w) {
    auto graph = mtd::Graph(n);
    for (const auto& [s, t, d] : graph_all.getEdges()) {
      if (w <= d) { graph.addArc(s, t); }
    }

    std::vector<int> dv(n);
    bfs(graph, 0, [&](auto f, auto t, auto) { dv[t] = dv[f] + 1; });
    return dv[n - 1];
  };

  auto w_max = mtd::binarySearch(0, static_cast<int>(1e9) + 1, [&](int w) {
    auto d = solve(w);
    return d > 0;
  });

  auto ans = solve(w_max);
  std::cout << w_max << " " << ans << std::endl;
}
Back to top page