Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- CommisvoyageurResult CommisvoyageurGreedy(T** w, size_t N, size_t vert_start) {
- auto start = std::chrono::high_resolution_clock::now();
- CommisvoyageurResult result;
- result.sum_path = 0;
- result.path.resize(N - 1);
- std::vector<bool> used(N, false);
- used[vert_start] = true;
- size_t vert_cur = vert_start;
- for (size_t i = 0; i < N - 1; ++i) {
- uint64_t weight_min = UINT64_MAX;
- size_t save_pos = 0;
- for (size_t j = 0; j < N; ++j) {
- T weight = w[vert_cur][j];
- if (j != vert_cur && !used[j] && weight > 0 &&
- static_cast<uint64_t>(weight) < weight_min) {
- weight_min = static_cast<uint64_t>(weight);
- save_pos = j;
- ops += 12;
- }
- ops += 25;
- }
- if (weight_min == UINT64_MAX) {
- throw std::runtime_error("Path not found!");
- }
- result.sum_path += weight_min;
- result.path[i] = save_pos;
- used[save_pos] = true;
- vert_cur = save_pos;
- ops += 18 + i;
- }
- result.sum_path += static_cast<uint64_t>(w[vert_cur][vert_start]);
- ops += 15 + N;
- auto stop = std::chrono::high_resolution_clock::now();
- result.time_calc = (stop - start).count();
- return result;
- }
Add Comment
Please, Sign In to add comment