54 std::vector<std::vector<uint32_t>>
59 std::uint64_t setOfCities,
63 std::vector<std::vector<uint32_t>>
67 if (setOfCities == (1 << n) - 1) {
72 if (
dp[setOfCities][city] != -1) {
73 return dp[setOfCities][city];
76 uint64_t ans = 2147483647;
77 for (
int choice = 0; choice < n; choice++) {
79 if ((setOfCities & (1 << choice)) ==
81 std::uint64_t subProb =
84 dist, setOfCities | (1 << choice), choice, n,
dp);
87 ans = std::min(ans, subProb);
90 dp[setOfCities][city] = ans;
102 std::vector<std::vector<uint32_t>> dist = {
103 {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}};
104 uint32_t V = dist.size();
105 std::vector<std::vector<uint32_t>>
dp(1 << V, std::vector<uint32_t>(V, -1));
106 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
107 travelling_salesman_using_bit_manipulation(dist, 1, 0, V,
dp) ==
109 std::cout <<
"1st test-case: passed!"
113 dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
115 std::vector<std::vector<uint32_t>> dp1(1 << V,
116 std::vector<uint32_t>(V, -1));
117 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
118 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) ==
120 std::cout <<
"2nd test-case: passed!"
123 dist = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}};
125 std::vector<std::vector<uint32_t>> dp2(1 << V,
126 std::vector<uint32_t>(V, -1));
127 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
128 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) ==
131 std::cout <<
"3rd test-case: passed!"