cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: Prim 法
(include/emthrm/graph/prim.hpp)

最小全域木 (minimum spanning tree)

時間計算量

$O(\lvert E \rvert \log{\lvert V \rvert})$

仕様

Prim 法

名前 戻り値 要件
template <typename CostType>
CostType prim(const std::vector<std::vector<Edge<CostType>>>& graph, const int root = 0);
根を $\mathrm{root}$ とするグラフ $\mathrm{graph}$ の最小全域木のコスト $\mathrm{graph}$ は双方向の辺の情報をもつ。

Kruskal 法

名前 戻り値 備考
template <typename CostType>
CostType kruskal(const std::vector<std::vector<Edge<CostType>>>& graph);
グラフ $\mathrm{graph}$ の最小全域木のコスト $\mathrm{graph}$ には単方向の辺の情報のみでよい。

参考文献

Prim 法

Kruskal 法

TODO

Submissons

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_PRIM_HPP_
#define EMTHRM_GRAPH_PRIM_HPP_

#include <functional>
#include <queue>
#include <vector>

#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType>
CostType prim(const std::vector<std::vector<Edge<CostType>>>& graph,
              const int root = 0) {
  const int n = graph.size();
  CostType res = 0;
  std::vector<bool> is_visited(n, false);
  is_visited[root] = true;
  std::priority_queue<Edge<CostType>,
                      std::vector<Edge<CostType>>,
                      std::greater<Edge<CostType>>> que;
  for (const Edge<CostType>& e : graph[root]) {
    if (e.dst != root) que.emplace(e);
  }
  while (!que.empty()) {
    const Edge<CostType> e1 = que.top();
    que.pop();
    if (is_visited[e1.dst]) continue;
    is_visited[e1.dst] = true;
    res += e1.cost;
    for (const Edge<CostType>& e2 : graph[e1.dst]) {
      if (!is_visited[e2.dst]) que.emplace(e2);
    }
  }
  return res;
}

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_PRIM_HPP_
#line 1 "include/emthrm/graph/prim.hpp"



#include <functional>
#include <queue>
#include <vector>

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 9 "include/emthrm/graph/prim.hpp"

namespace emthrm {

template <typename CostType>
CostType prim(const std::vector<std::vector<Edge<CostType>>>& graph,
              const int root = 0) {
  const int n = graph.size();
  CostType res = 0;
  std::vector<bool> is_visited(n, false);
  is_visited[root] = true;
  std::priority_queue<Edge<CostType>,
                      std::vector<Edge<CostType>>,
                      std::greater<Edge<CostType>>> que;
  for (const Edge<CostType>& e : graph[root]) {
    if (e.dst != root) que.emplace(e);
  }
  while (!que.empty()) {
    const Edge<CostType> e1 = que.top();
    que.pop();
    if (is_visited[e1.dst]) continue;
    is_visited[e1.dst] = true;
    res += e1.cost;
    for (const Edge<CostType>& e2 : graph[e1.dst]) {
      if (!is_visited[e2.dst]) que.emplace(e2);
    }
  }
  return res;
}

}  // namespace emthrm
Back to top page