题目
http://poj.org/problem?id=1276
题意
一台机器,里面有N种钱,第k种钱币值为Dk,共有Nk张。问不超过cash最多能取出多少钱。
思路
最最简单的可达性dp。
代码
Mem: 384K Time: 329MS
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 10;
const int MAXCASH = 1e5 + 1;
int nk[MAXN];
int dk[MAXN];
int n;
int cash;
bool used[MAXCASH];
int dp[MAXCASH];
int dpnum = 0;
int main() {
while (cin >> cash) {
memset(used, 0, sizeof(used));
dpnum = 0;
cin >> n;
for (int i = 0; i < n; i++)cin >> nk[i] >> dk[i];
used[0] = true;
dp[dpnum++] = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
int dpnow = dpnum;
for (int j = 0; j < dpnow; j++) {
int old_status = dp[j];
for (int k = 1; k <= nk[i] && k * dk[i] + old_status <= cash; k++) {
int new_status = k * dk[i] + old_status;
if (!used[new_status]) {
used[new_status] = true;
dp[dpnum++] = new_status;
if(new_status > ans) ans = new_status;
}
}
}
}
cout << ans << endl;
}
return 0;
}