DP解决背包问题(01背包/多重背包/完全背包)

【未完待续】

DP思想:

DP求解核心:状态转移方程和边界条件。

最朴素的01背包问题

注意点:

1. 理解dp[][]的意义,表示前i种物品在重量为j时的最大价值。

2. 在选择放入物品时,注意对背包大小判断。

DP解决背包问题(01背包/多重背包/完全背包)_第1张图片

DP解决背包问题(01背包/多重背包/完全背包)_第2张图片

//二维数组 
#include
#include
#include 
#define N 1001
using namespace std;
int dp[N][N];	//选择前i物品容量j的价值 
int w[N];	//重量 
int v[N];	//价值 
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);	//n是物品数,m是容量 
	for(int i=1;i<=n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&w[i]);
	}
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(j>=w[i]){	//如果要放进去,判断是否可以腾出该物品的空间 	
				dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]); 
			}
			else dp[i][j]=dp[i-1][j];
			
		}
	}
	printf("%d\n",dp[n][m]);
	return 0;
} 

01背包问题改进(一维数组)

注意点:

1. 考虑到上述代码种计算dp[i][j]时,实际上只需要dp[i-1][0:j]的元素,因此只用一维数组即可处理。并且注意到只有当j>=v[i](即背包大小大于当前物品的重量)才可能更新。通过这种方式可以减小空间复杂度。

//一维数组 
#include
#include 
#include
#define N 1001
using namespace std;
int dp[N];	//选择前i物品容量j的价值 
int w[N];	//重量 
int v[N];	//价值 
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);	//n是物品数,m是容量 
	for(int i=1;i<=n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&w[i]);
	}
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=n;i++){
		for(int j=m;j>=w[i];j--){
			dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
		}
	}
	printf("%d\n",dp[m]);
	return 0;
} 

题目描述

    有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值。     如,有1分,3分,3分,3分,4分五张邮票,要求凑成10分,则使用3张邮票:3分、3分、4分即可。

输入描述

有多组数据,对于每组数据,首先是要求凑成的邮票总值M,M<100。然后是一个数N,N<20,表示有N张邮票。接下来是N个正整数,分别表示这N张邮票的面值,且以升序排列。

输出描述

对于每组数据,能够凑成总值M的最少邮票张数。若无解,输出0。

输入

10
5
1 3 3 3 4

输出

3

#include
#include
#include
#define INF 1000
using namespace std;
int dp[101][101];//前i张达到j元 需要的张数 
int v[21];//每一张邮票的价格 
int main(void)
{
	int m,n;//凑成m元 有n张 
	while(scanf("%d%d",&m,&n)!=EOF){
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)	scanf("%d",&v[i]);
	 	for(int i=0;i<=n;i++) dp[i][0]=0;
		for(int j=1;j<=m;j++) dp[0][j]=INF;//不能从这个状态转移而来 
		dp[0][0]=0;
	 	for(int i=1;i<=n;i++){
	 		for(int j=1;j<=m;j++){
	 			if(j-v[i]>=0){
				 	dp[i][j]=min(dp[i-1][j],dp[i-1][j-v[i]]+1);//要不要第i张? 
				}
				else dp[i][j]=dp[i-1][j];
			 }
		} 
	 	if(dp[n][m]==INF) printf("0\n");
	 	else printf("%d\n",dp[n][m]);
	 	
	}
	return 0;
}

值得学习的链接:https://blog.csdn.net/wdy_yx/article/details/9495415 

完全背包问题

阐述:每种物品可以无限制的重复使用,可以选择放或不放。

Description

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid. 

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs! 

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams. 

Output

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.". 

Sample Input

3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4

Sample Output

The minimum amount of money in the piggy-bank is 60. The minimum amount of money in the piggy-bank is 100. This is impossible.

//完全背包 
//Piggy-bank
#include
#define INF 0x7fffffff
int min(int a,int b){
	return a

归纳

是否装满背包:除了dp[0][0]以外,dp[0][i]初始化为INF(这里转化为无穷大是因为状态转移方程要求的是较小值),表示不能从这个状态转移而来。

是否可以重复选择(只采用一维数组时):唯一的区别是(01背包)第2个for循环倒序,(多重背包)第2个for循环顺序。

多重背包问题

 

有助于理解的链接:https://blog.csdn.net/hiudawn/article/details/80138427

你可能感兴趣的:(算法基础)