Coins 多重背包
来源:互联网 发布:工业自动化编程 编辑:程序博客网 时间:2024/05/17 01:16
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.
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 101 2 4 2 1 12 51 4 2 10 0
Sample Output
84
题意是这样的,给你若干种金币,每种金币若干个,先给你一个数字,要求这些金币任意组合,使得总价值不多于这个数字。
显然这是一道背包类型的题目,属于多重背包;然而没有优化它是会超时的 ,故用二进制来处理它
代码:
#include<iostream>using namespace std;#define MAX(a,b) a>b?a:bint N,M;int dp[100005];int w[105],c[105];void ZeroOnePack(int w ,int weight )//01背包{int i;for(i=M;i>=w;i--){dp[i]=MAX(dp[i],dp[i-w]+weight);}}void CompletePack(int w,int weight)//完全背包{int i;for(i=w;i<=M;i++)dp[i]=MAX(dp[i],dp[i-w]+weight);}void MultiplePack(int w,int weight,int count)//多重背包{if(w*count>=M)//转化为完全背包CompletePack(w,weight);else//转化为01背包{int k=1;while(k<=count)//二进制优化{ZeroOnePack(k*w,k*weight);count-=k;k<<1;}ZeroOnePack(count*w,count*weight);}}int main(){int i,ans;while(~scanf("%d%d",&N,&M),M+N){ans=0;for(i=1;i<=M;i++)dp[i]=-100000000;dp[0]=0;for(i=1;i<=N;i++)scanf("%d",&w[i]);for(i=1;i<=N;i++)scanf("%d",&c[i]);for(i=1;i<=N;i++)MultiplePack(w[i],w[i],c[i]);for(i=1;i<=M;i++)if(dp[i]>0)ans++;printf("%d\n",ans);}return 0;}
- Coins 多重背包
- Coins(多重背包)
- HDU2844:Coins(多重背包)
- POJ1742:Coins(多重背包)
- Coins 多重背包
- hdu2844 Coins 多重背包
- G - Coins-------多重背包
- hdu2844 Coins --多重背包
- POJ1742-Coins-多重背包
- POJ1742 Coins 【多重背包】
- Coins 多重背包 HDU
- Coins (多重背包)
- Coins(多重背包)
- hdu 2844 Coins 多重背包
- hdu 2844 Coins 多重背包
- HDU 2844 Coins -- 多重背包
- hdu 2844 Coins - 多重背包
- hdu 2844 Coins(多重背包)
- HDOJ 2955Robberies(0 1 背包)
- poj 3903 Stock Exchange
- error LNK1123: failure during conversion to COFF的解决方法
- 大地测量员职业技能鉴定实操考核工作方案
- Flex入门(二)HelloWorld
- Coins 多重背包
- c++ premier -- 标准库类型
- mongodb启动不了
- hdu1851 A Simple Game-----sg
- 从深度缓冲里重建位置信息
- POJ 2506(放方块)
- wince6.0+s3c6410摄像头驱动修改 (分辨率)
- 高效 JavaScript
- Spring--知识点