|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <utility> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +template <class T> class csr_distance_matrix { |
| 8 | + |
| 9 | + int _rows = 0; |
| 10 | + std::vector<int> begins; |
| 11 | + std::vector<std::pair<int, T>> vals; |
| 12 | + |
| 13 | +public: |
| 14 | + csr_distance_matrix() : csr_distance_matrix({}, 0) {} |
| 15 | + |
| 16 | + csr_distance_matrix(const std::vector<std::tuple<int, int, T>> &init, int rows) |
| 17 | + : _rows(rows), begins(rows + 1) { |
| 18 | + std::vector<int> degs(rows); |
| 19 | + for (const auto &p : init) ++degs.at(std::get<0>(p)); |
| 20 | + |
| 21 | + for (int i = 0; i < rows; ++i) begins.at(i + 1) = begins.at(i) + degs.at(i); |
| 22 | + |
| 23 | + vals.resize(init.size(), std::make_pair(-1, T())); |
| 24 | + for (auto [i, j, w] : init) vals.at(begins.at(i + 1) - (degs.at(i)--)) = {j, w}; |
| 25 | + } |
| 26 | + |
| 27 | + void apply_pi(const std::vector<T> &pi) { |
| 28 | + for (int i = 0; i < n(); ++i) { |
| 29 | + for (auto &[j, d] : adjacents(i)) d += pi.at(i) + pi.at(j); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + int n() const noexcept { return _rows; } |
| 34 | + |
| 35 | + struct adjacents_sequence { |
| 36 | + csr_distance_matrix *ptr; |
| 37 | + int from; |
| 38 | + |
| 39 | + using iterator = typename std::vector<std::pair<int, T>>::iterator; |
| 40 | + iterator begin() { return std::next(ptr->vals.begin(), ptr->begins.at(from)); } |
| 41 | + iterator end() { return std::next(ptr->vals.begin(), ptr->begins.at(from + 1)); } |
| 42 | + }; |
| 43 | + |
| 44 | + struct const_adjacents_sequence { |
| 45 | + const csr_distance_matrix *ptr; |
| 46 | + const int from; |
| 47 | + |
| 48 | + using const_iterator = typename std::vector<std::pair<int, T>>::const_iterator; |
| 49 | + const_iterator begin() const { |
| 50 | + return std::next(ptr->vals.cbegin(), ptr->begins.at(from)); |
| 51 | + } |
| 52 | + const_iterator end() const { |
| 53 | + return std::next(ptr->vals.cbegin(), ptr->begins.at(from + 1)); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + adjacents_sequence adjacents(int from) { return {this, from}; } |
| 58 | + |
| 59 | + const_adjacents_sequence adjacents(int from) const { return {this, from}; } |
| 60 | + const_adjacents_sequence operator()(int from) const { return {this, from}; } |
| 61 | +}; |
| 62 | + |
| 63 | +template <class DistanceMatrix> auto build_adjacent_info(const DistanceMatrix &dist, int sz) { |
| 64 | + using T = decltype((*dist.adjacents(0).begin()).second); |
| 65 | + |
| 66 | + const std::vector<std::vector<T>> alpha = calc_lkh_alpha(dist); |
| 67 | + |
| 68 | + std::vector<std::tuple<int, int, T>> adjacent_edges; |
| 69 | + |
| 70 | + std::vector<std::tuple<T, T, int>> candidates; |
| 71 | + for (int i = 0; i < dist.n(); ++i) { |
| 72 | + candidates.clear(); |
| 73 | + for (auto [j, d] : dist.adjacents(i)) { |
| 74 | + if (i != j) candidates.emplace_back(alpha.at(i).at(j), d, j); |
| 75 | + } |
| 76 | + |
| 77 | + const int final_sz = std::min<int>(sz, candidates.size()); |
| 78 | + std::nth_element(candidates.begin(), candidates.begin() + final_sz, candidates.end()); |
| 79 | + |
| 80 | + candidates.resize(final_sz); |
| 81 | + std::sort(candidates.begin(), candidates.end(), |
| 82 | + [&](const auto &l, const auto &r) { return std::get<1>(l) < std::get<1>(r); }); |
| 83 | + for (auto [alpha, dij, j] : candidates) adjacent_edges.emplace_back(i, j, dij); |
| 84 | + } |
| 85 | + return csr_distance_matrix(adjacent_edges, dist.n()); |
| 86 | +} |
0 commit comments