Charlie's Change POJ

来源:互联网 发布:怎样注册网站域名 编辑:程序博客网 时间:2024/05/21 22:48

题目传送门

题意:给你四种硬币,硬币的面值分别为1,5,10,25,每一种硬币分别有C1,C2,C3,C4,问你能不能通过这些硬币构成p元,
(尽可能的多用硬币)如果能的话输出方案。

思路:完全背包记录一下路径就好了。

#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define MAXN 10100#define MAXE 10#define INF 100000000#define MOD 10001#define LL long long#define pi 3.14159using namespace std;int num[MAXN];int v[MAXE];int dp[MAXN];int used[MAXN];int path[MAXN][MAXE];int main() {    std::ios::sync_with_stdio(false);    LL p;    while (cin >> p >> num[1] >> num[2] >> num[3] >> num[4]) {        if (p == 0 && num[1] == 0 && num[2] == 0 && num[3] == 0 && num[4] == 0)            break;        memset(path, 0, sizeof(path));        v[1] = 1;        v[2] = 5;        v[3] = 10;        v[4] = 25;        dp[0] = 0;        for (int i = 1; i < MAXN; ++i) {            dp[i] = -INF;        }        for (int i = 1; i <= 4; ++i) {            memset(used, 0, sizeof(used));            for (int j = v[i]; j <= p; ++j) {                if (dp[j] < dp[j - v[i]] + 1 && used[j - v[i]] < num[i]) {                    dp[j] = dp[j - v[i]] + 1;                    used[j] = used[j - v[i]] + 1;                    for (int k = 1; k <= 4; ++k) {                        path[j][k] = path[j - v[i]][k];                    }                    path[j][i]++;                }            }        }        if (dp[p] < 0) {            cout << "Charlie cannot buy coffee.\n";        } else {            cout << "Throw in " << path[p][1] << " cents, " << path[p][2] << " nickels, " << path[p][3] << " dimes, and " << path[p][4] << " quarters.\n";        }    }    return 0;}/* */
原创粉丝点击