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 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.

Source

CTU Open 2003

给出了四种不同面值的硬币及其数量,问组成P价值所用的最多数量的硬币是多少,并输出这个方案。应该是多重背包把这个,用二进制优化个数之后就不会T了,刚开始卡了很久因为两层循环我写反了,一直没写过背包有点蒙,由于用到了一维数组的优化,所以对于当前的某件物品,当前的价值用到的子问题必须不涉及到这件物品。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 #define inf 0x3f3f3f3f
 6 int coin[5]={0,1,5,10,25};
 7 int f[10005];
 8 int book[5];
 9 struct date
10 {
11     int num,type;
12 }Q[10005];
13 int main()
14 {
15     int C[5],P,n,m,i,j,k;
16     while(cin>>P>>C[1]>>C[2]>>C[3]>>C[4]){
17         if(!(P+C[1]+C[2]+C[3]+C[4])) break;
18         memset(f,-inf,sizeof(f));
19         memset(Q,0,sizeof(Q));
20         memset(book,0,sizeof(book));
21         int W,L,l=0;
22         f[0]=0;
23         for(i=1;i<=4;++i)
24         {
25             l=0;
26             for(k=1;C[i];C[i]-=k,k*=2){
27                 if(C[i]<k) k=C[i];
28                // cout<<i<<‘ ‘<<k<<endl;
29                 W=k*coin[i];
30             for(j=P;j>=W;--j)
31             {
32
33
34                     if(f[j-W]!=-inf&&f[j]<f[j-W]+k)
35                         {
36                             f[j]=f[j-W]+k;
37                             Q[j].num=k;
38                             Q[j].type=i;
39                         }
40             }
41             }
42         }//puts("dd");
43        // cout<<f[P]<<endl;
44        if(f[P]<0) puts("Charlie cannot buy coffee.");
45        else{
46             j=P;
47             i=0;
48             while(j){
49                 book[Q[j].type]+=Q[j].num;
50                 j-=coin[Q[j].type]*Q[j].num;
51             }
52             printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",book[1],book[2],book[3],book[4]);
53        }
54     }
55     return 0;
56 }
时间: 2024-08-05 03:04:07

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 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

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

POJ 3414 Pots 记录路径的广搜

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

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