cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 巡回セールスマン問題 (traveling salesman problem)
(include/emthrm/graph/traveling_salesman_problem.hpp)

重み付き有向グラフに対してコスト最小のハミルトン閉路を求める問題である。

時間計算量

$O(2^{\lvert V \rvert} {\lvert V \rvert}^2)$

仕様

Held–Karp algorithm

名前 戻り値
template <typename CostType>
CostType traveling_salesman_problem(const std::vector<std::vector<Edge<CostType>>>& graph, const CostType inf = std::numeric_limits<CostType>::max());
グラフ $\mathrm{graph}$ の巡回セールスマン問題の解のコスト。ただし解が存在しないときは $\infty$ を返す。

参考文献

Held–Karp algorithm

TODO

Submissons

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_TRAVELING_SALESMAN_PROBLEM_HPP_
#define EMTHRM_GRAPH_TRAVELING_SALESMAN_PROBLEM_HPP_

#include <algorithm>
#include <limits>
#include <numeric>
#include <vector>

#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType>
CostType traveling_salesman_problem(
    const std::vector<std::vector<Edge<CostType>>>& graph,
    const CostType inf = std::numeric_limits<CostType>::max()) {
  const int n = graph.size();
  if (n == 1) [[unlikely]] return 0;
  std::vector<std::vector<CostType>> dp(1 << n, std::vector<CostType>(n, inf));
  dp[1][0] = 0;
  for (int i = 1; i < (1 << n); ++i) {
    for (int j = 0; j < n; ++j) {
      if (dp[i][j] == inf) continue;
      for (const Edge<CostType>& e : graph[j]) {
        if (i >> e.dst & 1) continue;
        dp[i | (1 << e.dst)][e.dst] =
            std::min(dp[i | (1 << e.dst)][e.dst], dp[i][j] + e.cost);
      }
    }
  }
  CostType res = inf;
  for (int j = 1; j < n; ++j) {
    if (dp.back()[j] == inf) continue;
    for (const Edge<CostType>& e : graph[j]) {
      if (e.dst == 0) res = std::min(res, dp.back()[j] + e.cost);
    }
  }
  return res;
}

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_TRAVELING_SALESMAN_PROBLEM_HPP_
#line 1 "include/emthrm/graph/traveling_salesman_problem.hpp"



#include <algorithm>
#include <limits>
#include <numeric>
#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 10 "include/emthrm/graph/traveling_salesman_problem.hpp"

namespace emthrm {

template <typename CostType>
CostType traveling_salesman_problem(
    const std::vector<std::vector<Edge<CostType>>>& graph,
    const CostType inf = std::numeric_limits<CostType>::max()) {
  const int n = graph.size();
  if (n == 1) [[unlikely]] return 0;
  std::vector<std::vector<CostType>> dp(1 << n, std::vector<CostType>(n, inf));
  dp[1][0] = 0;
  for (int i = 1; i < (1 << n); ++i) {
    for (int j = 0; j < n; ++j) {
      if (dp[i][j] == inf) continue;
      for (const Edge<CostType>& e : graph[j]) {
        if (i >> e.dst & 1) continue;
        dp[i | (1 << e.dst)][e.dst] =
            std::min(dp[i | (1 << e.dst)][e.dst], dp[i][j] + e.cost);
      }
    }
  }
  CostType res = inf;
  for (int j = 1; j < n; ++j) {
    if (dp.back()[j] == inf) continue;
    for (const Edge<CostType>& e : graph[j]) {
      if (e.dst == 0) res = std::min(res, dp.back()[j] + e.cost);
    }
  }
  return res;
}

}  // namespace emthrm
Back to top page