现在的位置: 首页 > 综合 > 正文

hdu2844之混合背包

2014年01月03日 ⁄ 综合 ⁄ 共 2729字 ⁄ 字号 评论关闭
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
 

a[i]代表第i个硬币的价值,b[i]代表第i个硬币的个数,dp[j]代表价值为j的手表是否能买, 当买价值为 j 的手表并且用第 i 个硬币时,  如果能恰好用硬币组合成 j-a[i]的价值,则价值为j也能由 j-a[i]+a[i]组成,即if(dp[j-a[i]])dp[j]=true;



#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=100002;
bool dp[MAX];//把m当做背包,dp[i]代表要买价值为i的物品是否能恰好用硬币买到.
int a[102],b[102];//a代表第i个硬币的价值,b代表个数. 

int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;++i)cin>>a[i];
		for(int i=0;i<n;++i)cin>>b[i];
		if(m<=0){cout<<"0\n";continue;}//这题很无语的会出现m为负数的情况.
		memset(dp,false,sizeof(bool)*(m+1));
		dp[0]=true;
		for(int i=0;i<n;++i){
			if(a[i]*b[i]>=m){//如果a[i]*b[i]>=m,则在背包容量m内,第i个硬币可以取任意n个(n*a[i]<=m)都不会超过b[i]个,所以可以看做为完全背包. 
				for(int j=a[i];j<=m;++j){
					if(dp[j-a[i]])dp[j]=true;
				}
			}
			else{//如果a[i]*b[i]<m,则在背包容量m内,第i个硬币最多可以取b[i]个,即多重背包. 
				int k=0;
				for(k=1;k*2-1<=b[i];k=k*2){//用二进制把b[i]分成1,2,4,8...2^(k-1),b[i]-2^k+1种物品,然后以01背包来做. 
					for(int j=m;j>=k*a[i];--j){
						if(dp[j-k*a[i]])dp[j]=true;
					}
				}
				for(int j=m;j>=(b[i]-k+1)*a[i];--j){
					if(dp[j-(b[i]-k+1)*a[i]])dp[j]=true;
				}
			}
		}
		int sum=0;
		for(int i=1;i<=m;++i)if(dp[i])sum++;
		cout<<sum<<endl;
	}
	return 0;
} 

也可以直接当做多重背包,不过效率稍微慢了些

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=100002;
bool dp[MAX];//把m当做背包,dp[i]代表要买价值为i的物品是否能恰好用硬币买到.
int a[102],b[102];//a代表第i个硬币的价值,b代表个数. 

int main(){
	int n,m,k;
	while(cin>>n>>m,n||m){
		for(int i=0;i<n;++i)cin>>a[i];
		for(int i=0;i<n;++i)cin>>b[i];
		if(m<=0){cout<<"0\n";continue;}//这题很无语的会出现m为负数的情况.
		memset(dp,false,sizeof(bool)*(m+1));
		dp[0]=true;
		for(int i=0;i<n;++i){ 
		    for(k=1;k*2-1<=b[i];k=k*2){//用二进制把b[i]分成1,2,4,8...2^(k-1),b[i]-2^k+1种物品,然后以01背包来做. 
		        for(int j=m;j>=k*a[i];--j){
		            if(dp[j-k*a[i]])dp[j]=true;
	            }
	        }
		    for(int j=m;j>=(b[i]-k+1)*a[i];--j){
		        if(dp[j-(b[i]-k+1)*a[i]])dp[j]=true;
		    }
		}
		int sum=0;
		for(int i=1;i<=m;++i)if(dp[i])sum++;
		cout<<sum<<endl;
	}
	return 0;
} 

【上篇】
【下篇】

抱歉!评论已关闭.