hdu 1114 Piggy-Bank 完全背包

来源:互联网 发布:大数据下的人力资源 编辑:程序博客网 时间:2024/06/16 04:23

传送门http://acm.split.hdu.edu.cn/showproblem.php?pid=1114

  • 题意:
    • 给出存钱罐当前的重量以及空罐的质量, 给出 n 种货币的价值和重量, 问存钱罐中总价值的最小可能是多少. (如果无法用 n 种货币凑出则 impossible);

裸背包. 不多解释.

#include <stdio.h>#include <iostream>#include <cstring>using namespace std;//#define localconst int MAX = 1e4+10;int T, E, F, n, c[MAX], w[MAX], dp[MAX];int max(int a, int b) {    return a > b ? a : b;}int min(int a, int b) {    return a < b ? a : b;}void getin() {    cin >> E >> F >> n;    for (int i = 0; i < n; ++i)        cin >> w[i] >> c[i];//      cin >> c[i] >> w[i];    F -= E;    memset (dp, 0x3f, sizeof (dp));    dp[0] = 0;    return ;}int judge() {    for (int i = 0; i <= F; ++i) {        for (int j = 0; j < n; ++j) {            if (i + c[j] > F)                   continue;            dp[i+c[j]] = min(dp[i+c[j]], dp[i] + w[j]);        }    }    return dp[F] < 0x3f3f3f3f ? dp[F] : -1;}int main() {#ifdef local    freopen("in.t", "r", stdin);#endif    ios::sync_with_stdio(false);    cin.tie(0);    cin >> T;    while (T--) {        getin();        int ans = judge();        if (ans >= 0)            cout << "The minimum amount of money"                     << " in the piggy-bank is "                         << ans << "." << endl;        else             cout << "This is impossible." << endl;    }    return 0;}

2017-10-16
(预祝南开中学 113 岁生日快乐)