标签:space its ons http cos air push alt highlight
比赛爆零==

简单来说 题意就是 给一个N 然后给了4种操作的代价 求最小的代价。用DFS搜索
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) {
ll n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
std::vector<pair<int, ll>> ops;
// ops.emplace_back(2, a);
ops.push_back(make_pair(2, a));
ops.push_back(make_pair(3, b));
ops.push_back(make_pair(5, c));
const ll inf = 4e18;
map<ll, ll> mp;
mp[0] = 0;
mp[1] = d;
function<ll(ll)> DFS = [&](ll x) {
if(mp.find(x) != mp.end()) { //如果找到了返回这个值
return mp[x];
}
ll ret = inf;
//不进行判断会wa
if((long double)d * (long double)x < ret) {
ret = d * x;
}
for(auto& p : ops) {
int num = p.first;
ll cost = p.second;
//分成了两种情况:第一种是减1;第二种是加1
ret = min(ret, DFS(x / num) + cost + d * (x % num));
if(x % num != 0) {
ret = min(ret, DFS(x / num + 1) + cost + d * (num - x % num));
}
}
return mp[x] = ret;
};
cout << DFS(n) << endl;
}
return 0;
}
标签:space its ons http cos air push alt highlight
原文地址:https://www.cnblogs.com/lightac/p/12952435.html