背包总结

本文介绍了01背包和完全背包问题的解决方法。01背包问题中,每种物品仅有一件,需要在有限容量内选取最有价值的组合;完全背包问题则允许无限数量的某种物品。通过动态规划策略,分别给出了两种问题的解决方案,并提供了相应的C++代码实现。在实际编程中,需要注意初始化dp数组的细节,以避免得到错误答案。
摘要由CSDN通过智能技术生成

我对背包问题的理解是在有限的容量内选择刚好装满且最有价值的物品。
本周我主要学习和掌握使用了01背包问题和完全背包问题,01背包问题的主要线索是每件物品仅有一件,每件都各有各的价值,选取之后就不能在选取,而完全背包则是本物品有无数件,可供你选择到将容量装满。
01背包问题
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
题意
这个人喜欢收藏骨头,有个容量为V的麻袋,装入一个骨骼占相应一块体积,每个骨骼都有自身或多或少的价值
需要要考虑的是到第i件物品时是否还需要放,dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i])
其实也就是通过比较放入与不放入的大小把价值大的留下来,然后把各个体积的价值留下来.可以看出来这个方程只需要知道前一个的数据,所以拿空间剩余量写取得的最大值也可以所以用dp[v]=max(dp[v],dp[v-w[i]]+c[i])

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t,n,V;
    int w[1001],v[1001];
    int dp[1001];
    cin>>t;
    while(t--)
    {
        cin>>n>>V;
        for(int i=0;i<n;i++)
            cin>>w[i];
        for(int i=0;i<n;i++)
        cin>>c[i];
        for(int i=0;i<=V;i++)
        {
            dp[i]=0;
        }

        for(int i=0;i<n;i++)
        {

            for(int j=V;j>=v[i];j--)
            {
                dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
            }
        }
        cout <<dp[V]<<endl;
    }
    return 0;
}

完全背包问题
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.
题意:你知道存钱罐的重量和装满硬币的重量,各种硬币的重量,问你在这个重量下,存钱罐中最小的钱数是多少。
不限数量,给你M和m,v价值,很明显就是完全背包。dp[i]表示重量为i的时候的最小价值!然后在做的时候,我没注意初始化的问题,习惯写成,结果答案全是零,需要求的是最小值,所以把dp全给赋为INF,但是对于dp[0],重量为0,价值肯定为0

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int dp[10001];
 int a[10001],b[10001];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int c,d;
		 cin>>c>>d;
		 int z=d-c;
		int n;
		cin>>n;
		memset(dp,inf,sizeof(dp));
		for(int i=1;i<=n;i++)
			cin>>a[i]>>b[i];
		dp[0]=0;
		for(int i=1;i<=n;i++)
		{
			for(int k=b[i];
                k<=z;
                 k++
               )
			{
				dp[k]=min(dp[k],dp[k-b[i]]+a[i]);
			}
		}
		if(dp[z]!=inf)
			cout<<"The minimum amount of money in the piggy-bank is "<<dp[z]<< "."<<endl;
		else
			cout<<"This is impossible."<<endl;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值