POJ - 1787 (多重背包还原路径|| 完全背包)

POJ  1787  Charlie‘s Change


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 motorests. Charlie hates change. That is basically the setup of your next task.

Your program will be given numbers and types of coins Charlie has and
the coffee price. The coffee vending machines accept coins of values 1,
5, 10, and 25 cents. The program should output which coins Charlie has
to use paying the coffee so that he uses as
many coins as possible. Because Charlie really does not want any change
back he wants to pay the price exactly.

Input

Each line of the input
contains five integer numbers separated by a single space describing one
situation to solve. The first integer on the line P, 1 <= P <= 10
000, is the coffee price in cents. Next four integers, C1, C2, C3,
C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5
cents), dimes (10 cents), and quarters (25 cents) in Charlie‘s valet.
The last line of the input contains five zeros and no output should be
generated for it.

Output

For each situation, your
program should output one line containing the string "Throw in T1 cents,
T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the
numbers of coins of appropriate values Charlie should use to
pay the coffee while using as many coins as possible. In the case
Charlie does not possess enough change to pay the price of the coffee
exactly, your program should output "Charlie cannot buy coffee.".

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

题意:给你一个物品的价值 n 然后给你 4种硬币的数量(1 5 10 25这四种硬币) 问最多用多少硬币能正好凑够n 

一眼看去就是多重背包还原路径这也是第一种解法

 1 //#include<bits/stdc++.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 using namespace std;
 7 #define inf 0x3f3f3f3f
 8 int vis[10001][90];
 9 int dp[10001];
10 int q[4];
11 int main()
12 {   freopen("in.txt","r",stdin);
13     ios::sync_with_stdio(false);
14     int n,c[4];
15     int v[4]={1,5,10,25};
16     while(cin>>n>>c[0]>>c[1]>>c[2]>>c[3]&&(n+c[1]+c[2]+c[3]+c[0]))
17     {
18         int a[101];
19         int b[101];
20         int d[101];
21         int cnt=0;
22         for(int i=0;i<4;i++)
23         {
24             int j=1;
25             while(j<=c[i])
26             {
27                     a[cnt]=j*v[i];
28                     b[cnt]=j;
29                     d[cnt]=i;
30                     cnt++;
31                     c[i]-=j;
32                     j*=2;
33             }
34             if(c[i])
35             {
36                     a[cnt]=c[i]*v[i];
37                     b[cnt]=c[i];
38                     d[cnt]=i;
39                     cnt++;
40             }
41
42
43         }
44
45         dp[0]=1;
46         for(int i=0;i<cnt;i++)
47         {
48
49             for(int j=n;j>=a[i];j--)
50             {
51                 if(dp[j-a[i]]&&dp[j]<dp[j-a[i]]+b[i])
52                 {
53                     dp[j]=dp[j-a[i]]+b[i];
54                     vis[j][i]=1;
55                 }
56
57             }
58
59         }
60     //    cout<<dp[n]<<endl;
61         if(!dp[n])
62         {
63             cout<<"Charlie cannot buy coffee."<<endl;
64         }
65         else
66         {
67             int j=n;
68             for(int i=cnt-1;i>=0;i--)
69             {
70                 if(vis[j][i])
71                 {  // cout<<d[i]<<endl;
72                     q[d[i]]+=b[i];
73                     j-=a[i];
74                 }
75
76             }
77             cout<<"Throw in "<<q[0]<<" cents, "<<q[1]<<" nickels, "<<q[2]<<" dimes, and "<<q[3]<<" quarters."<<endl;
78         }
79         for(int i=0;i<=n;i++)
80         {
81             dp[i]=0;
82             for(int j=0;j<cnt;j++)
83             vis[i][j]=0;
84         }
85         q[0]=q[1]=q[2]=q[3]=0;
86
87
88
89     }
90
91
92
93     return 0;
94 }

第二种完全背包 记录使用次数 大于时不更新

写的有点乱,就这吧,这种方法时间短(自己还是想不起来..)

  1 //#include<bits/stdc++.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdio>
  6 using namespace std;
  7 #define inf 0x3f3f3f3f
  8 int dp[10001];
  9 int q[105];
 10 int fa[10001];
 11 int usr[10001];
 12 int main()
 13 {   //freopen("in.txt","r",stdin);
 14     ios::sync_with_stdio(false);
 15     int n,c[6];
 16     int v[9]={0,1,5,10,25};
 17     while(cin>>n>>c[1]>>c[2]>>c[3]>>c[4]&&(n+c[1]+c[2]+c[3]+c[4]))
 18     {
 19       memset(fa,0,sizeof(fa));
 20       memset(dp,0,sizeof(dp));
 21       memset(q,0,sizeof(q));
 22      dp[0]=1;
 23         for(int i=1;i<=4;i++)
 24         {
 25           memset(usr,0,sizeof(usr));
 26             for(int j=v[i];j<=n;j++)
 27             {
 28
 29                 if(dp[j-v[i]]&&dp[j]<dp[j-v[i]]+1&&usr[j-v[i]]<c[i])
 30                 {
 31                     dp[j]=dp[j-v[i]]+1;
 32                     usr[j]=usr[j-v[i]]+1;
 33                     fa[j]=j-v[i];
 34                 }
 35
 36             }
 37
 38         }
 39         if(!dp[n])
 40         {
 41             cout<<"Charlie cannot buy coffee."<<endl;
 42         }
 43         else
 44         {
 45             while(n!=0)
 46             {
 47                 q[n-fa[n]]++;
 48                 n=fa[n];
 49
 50             }
 51             cout<<"Throw in "<<q[1]<<" cents, "<<q[5]<<" nickels, "<<q[10]<<" dimes, and "<<q[25]<<" quarters."<<endl;
 52         }
 53
 54         //q[0]=q[1]=q[2]=q[3]=0;
 55
 56
 57
 58     }
 59
 60
 61
 62     return 0;
 63 }
 64 /*
 65 #include<bits/stdc++.h>
 66 #include<iostream>
 67 #include<cstring>
 68 using namespace std;
 69 const int N = 10005;
 70
 71 struct ac{
 72     int w, v, c;
 73 }a[N*20];
 74
 75 int dp[N], w[8] = {0,1,5,10,25};
 76 int path[60][N], ans[105];
 77 int main()
 78 {    freopen("in.txt","r",stdin);
 79     cin.sync_with_stdio(false);
 80     int p,c[14];
 81     while(cin >> p >> c[1] >> c[2] >> c[3] >> c[4])
 82     {
 83         if(p+c[1]+c[2]+c[3]+c[4] == 0) break;
 84         int cnt = 0;
 85         for(int i = 1; i <= 4; i++)
 86         {
 87             for( int j = 1; j <= c[i]; j <<= 1)
 88             {
 89                 a[++cnt].w = j*w[i];
 90                 a[cnt].c = j;
 91                 a[cnt].v = w[i];
 92                 c[i] -= j;
 93             }
 94
 95             if(c[i])
 96             {
 97                 a[++cnt].w = w[i]*c[i];
 98                 a[cnt].c = c[i];
 99                 a[cnt].v = w[i];
100             }
101         }
102         memset(dp, 0, sizeof(dp));
103         memset(ans, 0, sizeof(ans));
104         for(int i = 1; i <= cnt; i++)
105         {
106             for(int j = 1; j <= p; j++) path[i][j] = 0;
107         }
108         cout<<cnt<<endl;
109         dp[0] = 1;
110         for(int i = 1; i <= cnt; i++)
111         {
112             for(int j = p; j >= a[i].w; j--)
113             {
114                 if(dp[j-a[i].w] && dp[j] < dp[j-a[i].w] + a[i].c)
115                 {
116                     dp[j] = dp[j-a[i].w] + a[i].c;
117                     path[i][j] = 1;
118                 }
119             }
120         }
121         //cout << dp[p] << endl;
122
123         if(dp[p])
124         {
125             for(int i = cnt; i >= 1; i--)
126             {
127                 if(path[i][p])
128                 {
129                     ans[a[i].v] += a[i].c;
130                     p -= a[i].w;
131                 }
132             }
133             cout << "Throw in " << ans[1] << " cents, " << ans[5] << " nickels, " << ans[10] << " dimes, and " << ans[25] << " quarters." << endl;
134         }
135         else cout << "Charlie cannot buy coffee.\n";
136
137     }
138     return 0;
139 }
140 */

原文地址:https://www.cnblogs.com/qq1976648487/p/8407199.html

时间: 2024-10-13 02:52:22

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

Charlie&#39;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

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 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

多重背包转换成完全背包和01背包

void CompletePack(int cost,int weight)   多重背包 { for(int i=cost;i<=m;i++) dp[i]=max(dp[i],dp[i-cost]+weight); } void ZeroOnePack(int cost,int weight)    01背包 { for(int i=m;i>=cost;i--) dp[i]=max(dp[i],dp[i-cost]+weight); } void MultiplyPack(int cost,

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"

多重背包问题(来源:背包九讲)

问题: 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本算法: 这题目和全然背包问题非常类似.主要的方程仅仅需将全然背包问题的方程稍微一改就可以,由于对于第i种物品有n[i]+1种策略:取0件,取1件--取n[i]件.令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值,则有状态转移方程:f[i][v]=max{f[i-1][v-k*c[i]]+k*w

Poj 1112 Rebuilding Roads(树形DP+背包)

题意:给你由N个点构成一颗树,问要孤立出一个有P个节点的子树最少需要删除多少条边.N的范围最大为150 N的范围不大,很容易想到在树上面做背包.把每个节点都看成一个背包,然后把每个儿子节点都看成是一组物品.为什么是一组呢,那是因为假设以儿子为根的节点的子树有S个节点,那么就有S+1种情况,要么将这整棵子树舍弃,要么从这个子树中取1-S个节点. 设f[i][j]为以i为根节点的子树,孤立出以i为根节点,一共含有j个节点的子树最少需要删除的边数(不包括删除i和他父亲的连接的那条边(假设i不是根节点)

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