diff --git a/Minimum Cost Path with Teleportations.cpp b/Minimum Cost Path with Teleportations.cpp new file mode 100644 index 0000000..4699634 --- /dev/null +++ b/Minimum Cost Path with Teleportations.cpp @@ -0,0 +1,325 @@ +const int debug = 0; + +#include +#include +#include +#include +#include + +using std::cout, std::endl, std::format; +using std::function, std::min, std::sort; +using std::vector; + +using coor_t = unsigned char; +using weight_t = unsigned short; +using cost_t = unsigned int; + +template +using ConstRef = const T &; + +template +using Ref = T &; + +struct Cons +{ + struct Coor + { + const static coor_t min = 0; + const static coor_t max = 79; + }; + + struct Weight + { + const static weight_t min = 0; + const static weight_t max = 10000; + }; + + struct Cost + { + const static cost_t min = 0; + const static cost_t max = Cons::Weight::max * 200; + }; +}; + +class MinInfoMgr +{ +private: + using InfoPair = std::pair; + using Iter = vector::iterator; + + vector m_info; + bool m_orged = false; + + static bool custom_cmp(ConstRef w1, ConstRef w2) + { + return w1.second < w2.second; + }; + +public: + /** + * Add a new info into min manager, will not organize. + */ + void add(ConstRef c, ConstRef w) + { + if (m_orged) + { + cout << "[Error] add() after organize()"; + exit(-1); + } + m_info.emplace_back(c, w); + } + + void organize() + { + m_orged = true; + + sort(m_info.begin(), m_info.end()); + + vector new_info; + new_info.reserve(m_info.size() + 10); + new_info.emplace_back(0, 0); + + for (const auto &p : m_info) + { + auto &cur = new_info.back(); + + if (p.first == cur.first) + { + cur.second = p.second; + continue; + } + // here, p.first > cur.first + if (p.second > cur.second) + { + new_info.emplace_back(p); + } + } + + m_info = new_info; + } + + /** + * Return minimum possible `cost` of the node from which could + * teleport into the node with given `weight`. + * + * If no valid cost data, return max_cost + 1. + */ + cost_t get(ConstRef weight) + { + if (!m_orged) + { + cout << "[Error] get() before organize()"; + exit(-1); + } + + auto it = std::lower_bound( + m_info.begin(), + m_info.end(), + std::make_pair(0, weight), + custom_cmp); + + if (it == m_info.end()) + { + return Cons::Cost::max + 1; + } + return (it->first); + } + + void clear(ConstRef r, ConstRef c) + { + int total = r * c; + + m_orged = false; + m_info.clear(); + m_info.reserve(r * c + 10); + } +}; + +class SubSolutionManager +{ +private: + /**Original node weight info */ + const vector> *m_p_weight; + /**Solution for sub-questions */ + cost_t m_cost_arr[11][Cons::Coor::max + 1][Cons::Coor::max + 1]; + MinInfoMgr m_min_mgr[11]; + +public: + using IterOp = function, ConstRef)>; + +public: + coor_t mr; + coor_t mc; + coor_t ml; + +public: + SubSolutionManager() {} + + /** + * Initialize solution manager based on solution space size info + */ + void clear(ConstRef>> grid, ConstRef teleport_limit) + { + + m_p_weight = &grid; + + mr = grid.size(); + mc = grid[0].size(); + ml = teleport_limit; + + for (int i = 0; i < 11; ++i) + { + m_min_mgr[i].clear(mr, mc); + } + + // for (coor_t i = 0; i <= ml; ++i) + // { + // for (coor_t j = 0; j < mr; ++j) + // { + // for (coor_t k = 0; k < mc; ++k) + // { + // m_cost_arr[i][j][k] = 0; + // } + // } + // } + } + + void check_idx(ConstRef r, ConstRef c) + { + if (debug == 0) + { + return; + } + + if (r >= mr || c >= mc) + { + cout << format("[Error] ({}, {}) out of bound, allowed: ({}, {})", r, c, mr, mc); + exit(-1); + } + } + + void check_layer(ConstRef layer) + { + if (debug == 0) + { + return; + } + + if (layer > ml) + { + cout << format("[Error] layer {} out of bound, allowed: {}", layer, ml); + exit(-1); + } + } + + /** + * Get sub-solution answer. + * Will automatically return max_cost + 1 for -1 indices. + * Throw error when out of range otherwise. + */ + cost_t get(ConstRef l, ConstRef r, ConstRef c) + { + if (r == coor_t(-1) || c == coor_t(-1)) + { + return Cons::Cost::max + 1; + } + + check_idx(r, c); + check_layer(l); + + return m_cost_arr[l][r][c]; + } + + /** + * Get weight of a node. + * Throw if out of range. + */ + weight_t get_weight(ConstRef r, ConstRef c) + { + check_idx(r, c); + return (*m_p_weight)[r][c]; + } + + /** + * Optimal transfer cost when only considering normal move. + * Zero index allowed. + */ + cost_t optimal_trans_normal(ConstRef l, ConstRef r, ConstRef c) + { + return min(get(l, r, c - 1), get(l, r - 1, c)) + weight_t(get_weight(r, c)); + } + + /** + * Optimal transfer cost when only considering transportation move. + * Only work when layer >= 1, otherwise, return max_cost + 1. + */ + cost_t optimal_trans_transport(ConstRef l, ConstRef r, ConstRef c) + { + if (l < 1) + { + return Cons::Cost::max + 1; + } + + return m_min_mgr[l - 1].get(get_weight(r, c)) + 0; + } + + cost_t optimal_trans(ConstRef l, ConstRef r, ConstRef c) + { + if (r == 0 && c == 0) + { + return 0; + } + + return min(optimal_trans_normal(l, r, c), optimal_trans_transport(l, r, c)); + } + + void solve() + { + for (coor_t cur_l = 0; cur_l <= ml; ++cur_l) + { + if (cur_l > 0) + { + m_min_mgr[cur_l - 1].organize(); + } + for (coor_t r = 0; r < mr; ++r) + { + for (coor_t c = 0; c < mc; ++c) + { + if (debug >= 2) + { + cout << format("[Iter] Node {}, {}\n", r, c); + } + + weight_t weight = get_weight(r, c); + + // no matter which layer, 0,0 cost nothing. + cost_t optimal = optimal_trans(cur_l, r, c); + + // update solution / min + m_cost_arr[cur_l][r][c] = optimal; + m_min_mgr[cur_l].add(optimal, weight); + } + } + } + } + + cost_t get_final() + { + return get(ml, mr - 1, mc - 1); + } +}; + +class Solution +{ +private: + SubSolutionManager mSolMgr; + +public: + int minCost(vector> &grid, int k) + { + // init + mSolMgr.clear(grid, k); + mSolMgr.solve(); + return mSolMgr.get_final(); + } +}; \ No newline at end of file