DP 完全背包&01背包 HDU-1114&HDU-2602 dp初步背包问题

一、完全背包

完全背包是不同规格物品有无限个,取用哪些装入背包里,使最大/装满时最小,之类的问题。

HDU-1114

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34039    Accepted Submission(s): 16886

 

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

 

题目大意:向小猪存钱罐里投币,求装满时可能的最小价值。

注意,完全背包的特征是同一物品有无限个这样子。

so,我们可以用dp[i][j]表示i及其之前物品挑选之后,j容量的背包里,总硬币的最小价值。

每挑选一个新的物品,都从w[i]开始遍历至满,遍历到后面的时候前面已经更新过了,所以会考虑进重复添加的情况。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxc=10000;
const int maxn=500;
const int inf=0x3f3f3f3f;

long long dp[maxc+10];

int main()
{
	int t;
	int e,f,c,n;
	int w[maxn+10],p[maxn+10];

	scanf("%d",&t);
	while(t--){
		//memset(dp,0,sizeof(dp));
		scanf("%d%d",&e,&f);
		c=f-e;
		scanf("%d",&n);
		for(int i=1;i<=n;++i){
			scanf("%d%d",&p[i],&w[i]);
		}
		for(int j=0;j<=c;++j)
			dp[j]=inf;
		dp[0]=0;
		for(int i=1;i<=n;++i){
			for(int j=w[i];j<=c;++j){	//完全背包,之后也可无限加
				dp[j]=min(dp[j],dp[j-w[i]]+p[i]);	//两种状态:不放或者放//开二维就成了01背包,每个相当于只能加一次
			}
		}
		if(dp[c]!=inf)
			printf("The minimum amount of money in the piggy-bank is %lld.\n",dp[c]);
		else
			printf("This is impossible.\n");
	}
}

二、01背包

01背包则是给各种不同规格的物品,每样物品只有一个,只有选或者不选。

HDU-2602

Bone Collector

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

 

Problem Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input

The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

 

Output

One integer per line representing the maximum of the total value (this number will be less than 231).

 

Sample Input

1

5 10

1 2 3 4 5

5 4 3 2 1

Sample Output

14

 

题目大意:骨头收藏家收集各种各样的骨头,求它背包里装下的骨头的最大价值。

01背包的遍历及状态转换方程与完全背包没有太大差别,主要是某一物品的是从满到c[i]倒着遍历的,这样遍历大容量的时候前面还没有更新过,这样子就不会把这件物品重复累加了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxnv=1000;

int main()
{
	int t;
	int n,v;
	int p[maxnv+10],c[maxnv+10];
	long long dp[maxnv+10];

	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof(dp));
		scanf("%d%d",&n,&v);
		for(int i=1;i<=n;++i)
			scanf("%d",&p[i]);
		for(int j=1;j<=n;++j)
			scanf("%d",&c[j]);
		for(int i=1;i<=n;++i){
			for(int j=v;j>=c[i];--j){	//01背包,不会重复更新
				dp[j]=max(dp[j],dp[j-c[i]]+p[i]);
			}
		}
		printf("%lld\n",dp[v]);
	}

	return 0;
}
//背包不需要排序

最后,背包和贪心不同,不需要排序啥的这样子(绝望脸

猜你喜欢

转载自blog.csdn.net/DADDY_HONG/article/details/81394242
今日推荐