码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-02-02 23:20:19      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:ati   line   color   ram   string   log   ret   还原   include   

 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 }
View Code

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

写的有点乱,就这吧,这种方法时间短(自己还是想不起来..)
技术分享图片
  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 */
View Code

 

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

标签:ati   line   color   ram   string   log   ret   还原   include   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!