Charlie's Change POJ - 1787 (完全背包+记录路径)

完全背包输出路径;对于每一次更新记录一下路径;注意钱币个数;

dp[i][0]代表在空间为i时需要多少枚钱币

dp[i][1]用来记录路径

cheek[j]用来记录在j时用了多少i枚钱币

思路在代码中;

  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))

int dp[10010][2],b[10]= {0,1,5,10,25};

int mub[10],cheek[10010],ans[10010];

int n;

int main()
{
    while(scanf("%d",&n))
    {
        int f=0;
        for(int i=1; i<=4; i++)
        {
            scanf("%d",&mub[i]);
            if(mub[i]==0) f++;
        }
        if(f==4&&!n) break;
        mem(dp,0);

        for(int i=1; i<=4; i++)
        {
            mem(cheek,0);
            for(int j=b[i]; j<=n; j++)
            {
                if(dp[j-b[i]][0]+1>dp[j][0]&&cheek[j-b[i]]+1<=mub[i]&&(j==b[i]||dp[j-b[i]][0]))//&&(j==b[i]||!dp[j][0]))
                                                                    //保证这次状态是从上一次合法状态推过来的
                {
                    dp[j][0]=dp[j-b[i]][0]+1;//记录钱币数
                    dp[j][1]=j-b[i];//记录路径
                    cheek[j]=cheek[j-b[i]]+1;//记录使用多少i枚钱币,防止超出
                }
            }
        }

        mem(ans,0);
        int i=n;
        if(!dp[n][ 0]) printf("Charlie cannot buy coffee.\n");
        else
        {
            while(i)//跑一边路径记录答案
            {
                //cout<<i<<endl;
                ans[i-dp[i][1]]++;
                i=dp[i][1];

            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[1],ans[5],ans[10],ans[25]);
        }
    }
    return 0;
}

Charlie's Change POJ - 1787 (完全背包+记录路径)

原文地址:https://www.cnblogs.com/minun/p/10908353.html

时间: 2024-12-17 20:04:47

Charlie's Change POJ - 1787 (完全背包+记录路径)的相关文章

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your

poj1787Charlie&#39;s Change(多重背包+记录路径+好题)

Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3720   Accepted: 1125 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motore

poj 1787 背包+记录路径

http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4512   Accepted: 1425 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at co

POJ 1293 Duty Free Shop(背包记录路径)

Description Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Ea

poj 1166 The Clocks 记录路径的广搜

题意: 给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点). 分析: 明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点.2.queue<node>比queue<int>要多花1别的时间. //poj 1166 //sep9 #include <iostream> #include

poj1417 带权并查集 + 背包 + 记录路径

True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exha

UVA 624(01背包记录路径)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 记录路径可以用一个二维数组,记录改变时的量.然后从后往前可以推得所有的值. #include <iostream> #include <string> #include <cstring> #include <cstdlib> #incl

UVA624(01背包记录路径)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 01背包打印路径. #include <cstdio> #include <cstring> using namespace std; const int MAXN=10005; int n,W; int w[30]; int dp[MAXN]; in

Pots POJ - 3414 (搜索+记录路径)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f