hdu2844 Coins(多重背包+装满背包的最大值)

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3287    Accepted Submission(s): 1293


Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
 

 

Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 

 

Output
For each test case output the answer on a single line.
 

 

Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
 

 

Sample Output
8
4
 
Source
 
题意 :给你一些不同价值和一定数量的硬币,求用这些硬币可以组合成价值在[1 , m]之间的有多少。
分析:初始 d[] 为负无穷,然后多重背包,最后统计d[]中有多少是大于0的。
 
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define MAXV 100001
 5 #define MAXN 101
 6 #define INF -100000000
 7 
 8 using namespace std;
 9 
10 int d[MAXV],weight[MAXN],cnt[MAXN],V;
11 
12 void OneZeroPack(int c,int w)
13 {
14     int i;
15     for(i=V; i>=c; i--)
16         d[i]=max(d[i],d[i-c]+w);
17 }
18 
19 void CompletePack(int c,int w)
20 {
21     int i;
22     for(i=c; i<=V; i++)
23         d[i]=max(d[i],d[i-c]+w);
24 }
25 
26 void MultPack(int c,int w,int n)
27 {
28     if(n*c>=V)
29     {
30         CompletePack(c,w);
31         return ;
32     }
33     int k=1;
34     while(k<=n)
35     {
36         OneZeroPack(k*c,k*w);
37         n=n-k;
38         k=k*2;
39     }
40     OneZeroPack(n*c,n*w);
41 }
42 
43 int main()
44 {
45     int n,i;
46     while(scanf("%d %d",&n,&V)==2&&(n||V))
47     {
48         for(i=0;i<=V;i++) d[i]=INF;
49         d[0]=0;
50         for(i=0;i<n;i++)
51             scanf("%d",&weight[i]);
52         for(i=0;i<n;i++)
53             scanf("%d",&cnt[i]);
54         for(i=0;i<n;i++)
55             MultPack(weight[i],weight[i],cnt[i]);
56         int ans=0;
57         for(i=1;i<=V;i++)
58             if(d[i]>0)
59                 ans++;
60         printf("%d\n",ans);
61     }
62     return 0;
63 }

 

posted @ 2012-08-13 09:20  mtry  阅读(1677)  评论(0)    收藏  举报