Advertisement
rudolf222222

Untitled

Nov 30th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. int Power(int x, int n) {
  6.   int result = 1;
  7.   while (n > 0) {
  8.     if (n & 1 == 1) {
  9.       result = result * x;
  10.     }
  11.     x = x * x;
  12.     n = n >> 1;
  13.   }
  14.   return result;
  15. }
  16. int ClearBit(int n, int k) { return (n & (~(1 << (k)))); }
  17. using Patrix = std::vector<std::vector<std::pair<int, int>>>;
  18. template <typename T>
  19. bool allEqual(std::vector<T> const& v) {
  20.   if (v.size() == 0) {
  21.     return false;
  22.   }
  23.   return std::all_of(v.begin(), v.end(),
  24.                      [&](T const& e) { return e == v.front(); });
  25. }
  26. bool Zeros(std::vector<int>& v) { return v[0] == 0 && allEqual(v); }
  27. using Matrix = std::vector<std::vector<int>>;
  28.  
  29. class Graph {
  30.  private:
  31.   Matrix dp_;
  32.   Matrix cities_;
  33.   int size_;
  34.   Matrix paths_;
  35.   std::pair<int, std::pair<int, int>> FindCheapest(int i, int mask) {
  36.     if (mask == (1 << size_) - 1) {
  37.       return {cities_[i][0], {i, mask}};
  38.     }
  39.     if (dp_[mask][i] != -1) {
  40.       return {dp_[mask][i], {i, mask}};
  41.     }
  42.     int res = INT32_MAX;
  43.     for (int j = 0; j < size_; ++j) {
  44.       if ((mask & (1 << j)) == 0) {
  45.         res = std::min(
  46.             res, FindCheapest(j, (mask ^ (1 << j))).first + cities_[i][j]);
  47.       }
  48.     }
  49.     dp_[mask][i] = res;
  50.     return {dp_[mask][i], {i, mask}};
  51.   }
  52.   std::pair<int, std::pair<int, int>> Total(int src) {
  53.     std::pair<int, std::pair<int, int>> res;
  54.     res = FindCheapest(src, 1);
  55.     return res;
  56.   }
  57.  
  58.  public:
  59.   Graph(Matrix& matrix) {
  60.     cities_ = matrix;
  61.     size_ = static_cast<int>(cities_.size());
  62.     dp_.assign(Power(2, size_), std::vector<int>(size_, -1));
  63.     paths_.assign(size_, std::vector<int>(size_, 0));
  64.   }
  65.   int FindBest() {
  66.     std::vector<int> res;
  67.     for (int i = 0; i < size_; ++i) {
  68.       if (Zeros(cities_[i])) {
  69.         res.push_back(0);
  70.         int j = 0;
  71.         for (std::vector<int>::iterator it = paths_[i].begin();
  72.              it != paths_[i].end(); ++it) {
  73.           *it = j++;
  74.         }
  75.       } else {
  76.         res.push_back(Total(i).first);
  77.         auto pr = Total(i);
  78.         int mask = pr.second.second;
  79.         int index = pr.second.first;
  80.         // int start = pr.second.first;  // last city
  81.         std::vector<int> path;
  82.         // path.push_back(start);
  83.         // mask = ClearBit(mask, index);
  84.         while (mask != 0) {
  85.           for (int j = 0; j < size_; ++j) {
  86.             if (dp_[mask][index] ==
  87.                 dp_[ClearBit(mask, index)][j] + cities_[index][j]) {
  88.               path.push_back(j);
  89.               index = j;
  90.               mask = ClearBit(mask, index);
  91.             }
  92.           }
  93.         }
  94.         for (auto& vec : path) {
  95.           std::cout << vec << " ";
  96.         }
  97.         std::cout << "\n";
  98.       }
  99.     }
  100.     int result = *std::min_element(res.begin(), res.end());
  101.     return result;
  102.   }
  103. };
  104.  
  105. void Requests() {
  106.   int n = 0;
  107.   std::cin >> n;
  108.   Matrix matrix(n, std::vector<int>(n, 0));
  109.   for (auto& vec : matrix) {
  110.     for (auto& elem : vec) {
  111.       std::cin >> elem;
  112.     }
  113.   }
  114.   Graph graph(matrix);
  115.   std::cout << graph.FindBest() << "\n";
  116. }
  117. int main() {
  118.   Requests();
  119.   return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement