Files
python/GESP/Mkx/ZC_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
975 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 15;
const int INF = 0x3f3f3f3f;
int a[N];
int b[100010]; // b[i]记忆化数组记录走了i这些公里情况下使用的最少钱数
/*
12 20 30 40 25 60 70 80 90 11
21
34
*/
// m是剩余多少公里 sum已经花了多少钱
// int sum=dfs(int m) 要走m公里最少需要多少钱=sum
int dfs(int m) { // 要走m公里最少需要花多少钱
if (b[m] > 0) // 如果记录过m公里最少需要花多少钱那么直接拿来就用,如果没有计算过,那就开始算吧
return b[m];
if (m == 0)
return 0;
int res = INF;
for (int i = 1; i <= min(m, 10); i++)
res = min(res, dfs(m - i) + a[i]);
// 答案记录上,省的再次计算
b[m] = res;
return res;
}
int main() {
for (int i = 1; i <= 10; i++)
cin >> a[i];
int m; // 要走多少公里
cin >> m;
// 递归
cout << dfs(m) << endl;
return 0;
}