325 lines
7.5 KiB
C++
325 lines
7.5 KiB
C++
const int debug = 0;
|
|
|
|
#include <vector>
|
|
#include <functional>
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <iostream>
|
|
|
|
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 <typename T>
|
|
using ConstRef = const T &;
|
|
|
|
template <typename T>
|
|
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<cost_t, weight_t>;
|
|
using Iter = vector<InfoPair>::iterator;
|
|
|
|
vector<InfoPair> m_info;
|
|
bool m_orged = false;
|
|
|
|
static bool custom_cmp(ConstRef<InfoPair> w1, ConstRef<InfoPair> w2)
|
|
{
|
|
return w1.second < w2.second;
|
|
};
|
|
|
|
public:
|
|
/**
|
|
* Add a new info into min manager, will not organize.
|
|
*/
|
|
void add(ConstRef<cost_t> c, ConstRef<weight_t> 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<InfoPair> 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_t> 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<coor_t> r, ConstRef<coor_t> 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<vector<int>> *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<void(ConstRef<coor_t>, ConstRef<coor_t>)>;
|
|
|
|
public:
|
|
coor_t mr;
|
|
coor_t mc;
|
|
coor_t ml;
|
|
|
|
public:
|
|
SubSolutionManager() {}
|
|
|
|
/**
|
|
* Initialize solution manager based on solution space size info
|
|
*/
|
|
void clear(ConstRef<vector<vector<int>>> grid, ConstRef<coor_t> 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<coor_t> r, ConstRef<coor_t> 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<coor_t> 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<coor_t> l, ConstRef<coor_t> r, ConstRef<coor_t> 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<coor_t> r, ConstRef<coor_t> 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<coor_t> l, ConstRef<coor_t> r, ConstRef<coor_t> 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<coor_t> l, ConstRef<coor_t> r, ConstRef<coor_t> 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<coor_t> l, ConstRef<coor_t> r, ConstRef<coor_t> 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<vector<int>> &grid, int k)
|
|
{
|
|
// init
|
|
mSolMgr.clear(grid, k);
|
|
mSolMgr.solve();
|
|
return mSolMgr.get_final();
|
|
}
|
|
}; |