MrS4g0

Untitled

Nov 14th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. template<typename T>
  2. CommisvoyageurResult CommisvoyageurGreedy(T** w, size_t N, size_t vert_start) {
  3.     auto start = std::chrono::high_resolution_clock::now();
  4.  
  5.     CommisvoyageurResult result;
  6.     result.sum_path = 0;
  7.     result.path.resize(N - 1);
  8.  
  9.     std::vector<bool> used(N, false);
  10.     used[vert_start] = true;
  11.  
  12.     size_t vert_cur = vert_start;
  13.     for (size_t i = 0; i < N - 1; ++i) {
  14.         uint64_t weight_min = UINT64_MAX;
  15.         size_t save_pos = 0;
  16.  
  17.         for (size_t j = 0; j < N; ++j) {
  18.             T weight = w[vert_cur][j];
  19.             if (j != vert_cur && !used[j] && weight > 0 &&
  20.                 static_cast<uint64_t>(weight) < weight_min) {
  21.                 weight_min = static_cast<uint64_t>(weight);
  22.                 save_pos = j;
  23.                 ops += 12;
  24.             }
  25.             ops += 25;
  26.         }
  27.  
  28.         if (weight_min == UINT64_MAX) {
  29.             throw std::runtime_error("Path not found!");
  30.         }
  31.  
  32.         result.sum_path += weight_min;
  33.         result.path[i] = save_pos;
  34.  
  35.         used[save_pos] = true;
  36.         vert_cur = save_pos;
  37.         ops += 18 + i;
  38.     }
  39.  
  40.     result.sum_path += static_cast<uint64_t>(w[vert_cur][vert_start]);
  41.  
  42.     ops += 15 + N;
  43.  
  44.     auto stop = std::chrono::high_resolution_clock::now();
  45.     result.time_calc = (stop - start).count();
  46.  
  47.     return result;
  48. }
Add Comment
Please, Sign In to add comment