题意:给定一能容纳一定重量的储钱罐 和 各种面额硬币的重量(数量无穷), 问恰好装满储钱罐时,罐内价值总和之最小。
思路:多重背包模板题。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 500 + 10;
int v[MAXN], w[MAXN];
int dp[10000 + 10];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while(t--)
{
int e, f; cin >> e >> f;
int n; cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> v[i] >> w[i];
}
memset(dp, INF, sizeof(dp));
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = w[i]; j <= (f - e); j++)
{
dp[j] = min(dp[j], dp[j - w[i]] + v[i]);
}
}
if(dp[f - e] != INF) cout << "The minimum amount of money in the piggy-bank is " << dp[f- e] << "." << endl;
else cout << "This is impossible." << endl;
}
}

本文介绍了一种解决特定多重背包问题的方法,通过使用C++实现的程序代码来找到在给定储钱罐容量和多种硬币重量的情况下,使得储钱罐恰好装满时罐内硬币价值总和最小的方案。采用动态规划思想,通过迭代更新状态数组来求解最优解。
228

被折叠的 条评论
为什么被折叠?



