HDU1114 动态规划 背包 多重背包

来源:互联网 发布:网络金融产品都有哪些 编辑:程序博客网 时间:2024/06/15 17:16

题意:给定一能容纳一定重量的储钱罐 和 各种面额硬币的重量(数量无穷), 问恰好装满储钱罐时,罐内价值总和之最小。
思路:多重背包模板题。

#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;    }}
阅读全文
1 0
原创粉丝点击