背包之01,完全,多重模板

5 篇文章 0 订阅
4 篇文章 0 订阅

贴一个自认为讲解不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.html

Bone Collector

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 58   Accepted Submission(s) : 25
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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 2 31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14
  1. /* 
  2. 01背包问题 
  3. 01背包问题的特点是,">每种物品仅有一件,可以选择放或不放。 
  4. 01背包问题描述: 
  5. 有N件物品和一个容量为V的背包。第i件物品的重量是c[i],价值是w[i]。 
  6. 求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。 
  7. */  
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int value[1001],volume[1001],dp[1001];
int  Max(int  x,int  y)
{
    return (x>y)?x:y;
}
int main()
{
   int t,n,V;
  scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&V);
       for(int i=0;i<n;i++)
        scanf("%d",&value[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&volume[i]);
            memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
            for(int j=V;j>=volume[i];j--)
        dp[j]=Max(dp[j],dp[j-volume[i]]+value[i]);//0-1背包,要么不装,要么装,体积减小,价值增大
        printf("%d",dp[V]);
   }
    return 0;
}
完全背包:

Piggy-Bank

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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
  1. /* 
  2. 完全背包问题的特点是,每种物品可以无限制的重复使用,可以选择放或不放。 
  3. 完全背包问题描述: 
  4. 有N物品和一个容量为V的背包。第i件物品的重量是wei[i],价值是val[i]。 
  5. */  
#include <iostream>
#include<cstdio>
using namespace std;
#define INF 1000000
#define maxn 501
int wei1,wei2,wei,w[maxn],dp[maxn],p[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&wei1,&wei2);
        wei=wei2-wei1;
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d%d",&p[i],&w[i]);
        for(int i=0; i<=wei; i++)
            dp[i]=INF;
        dp[0]=0;
        for(int i=0; i<n; i++)
            for(int j=w[i]; j<=wei; j++)
                dp[j]=min(dp[j],dp[j-w[i]]+p[i]);
        if(dp[wei]!=INF)
            printf("The minimum amount of money in the piggy-bank is %d.\n", dp[wei]);
        else printf("This is impossible.\n");
    }

    return 0;
}
3多重背包:
 
 
  1. //多重背包(MultiplePack): 有N种物品和一个容量为V的背包。  
  2. //第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。  
  3. //求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,  
  4. //且价值总和最大。  
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define maxn 101
int max(int x,int y)
{
    return x>y?x:y;
}
int p[maxn],w[maxn],c[maxn],dp[maxn];
int t,n,m;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&m,&n);//m是总金额,n是大米总共类数
        for(int i=0; i<n; i++)
            scanf("%d%d%d",&p[i],&w[i],&c[i]);//p[i]是单价,w[i]每袋质量,c[i]是袋数
        for(int i=0; i<n; i++)//i<n是大米总共类数
            for(int j=0; j<c[i]; j++)//c[i]是每类大米的袋数
                for(int k=m; k>=p[i]; k--)//k是总金额,只要是大于单价,总金额减减
                    dp[k]=max(dp[k],dp[k-p[i]]+w[i]);//求出最大的质量
                              printf("%d\n",dp[m]);
                }
              return 0;
}


### 回答1: 多重背包问题是指在给定容量和物品的价值和重量的情况下,如何最大限度地装入物品,使得总价值最大化的问题。它的模板是:给定N种物品和一个容量为V的背包,每种物品有无限件可用,每件物品的重量是w[i],其价值是v[i]。求解将哪些物品装入背包可使价值总和最大。 ### 回答2: 多重背包问题是一个经典的组合优化问题,它是在0/1背包问题的基础上进行了扩展。在多重背包问题中,每个物品可以被选择的次数不再是1次,而是有一个确定的上限k次(k>1)。我们需要选择一些物品放入背包中,使得它们的总体积不超过背包的容量,并且使得它们的总价值最大化。 要解决多重背包问题,可以使用动态规划的方法。首先,我们定义一个二维数组dp[i][j],其中i表示前i个物品,j表示背包的容量。dp[i][j]表示当只考虑前i个物品、背包容量为j时,能够获取的最大价值。然后,我们可以使用如下的状态转移方程来计算dp[i][j]的值: dp[i][j] = max(dp[i-1][j], dp[i-1][j-v[i]]+w[i], dp[i-1][j-2v[i]]+2w[i], ..., dp[i-1][j-kv[i]]+kw[i]) 其中,v[i]表示第i个物品的体积,w[i]表示第i个物品的价值,k表示第i个物品的可选次数。上述状态转移方程的意义是,我们可以选择不取第i个物品,或者分别取1次、2次、...、k次第i个物品,选择这些情况下的最大价值。 最后,我们可以通过遍历所有的物品和背包容量,计算出dp[n][m],其中n表示物品的个数,m表示背包的容量。dp[n][m]即为问题的解,表示只考虑前n个物品、背包容量为m时能够获取的最大价值。 综上所述,多重背包问题的解决方法是利用动态规划,通过定义状态转移方程和计算数组dp的值,找到问题的最优解。希望以上介绍对您有所帮助。 ### 回答3: 多重背包问题是常见的背包问题之一,与0-1背包问题和完全背包问题类似,但有一些区别。 在多重背包问题中,给定n个物品和一个容量为V的背包,每个物品有两个属性:重量w和价值v。同时,每个物品还有对应的个数限制c,表示该物品的数量最多可以选择c次。 我们需要选择物品放入背包,使得背包的总容量不超过V,同时物品的总价值最大。 多重背包问题可以用动态规划来解决。 我们可以定义一个二维数组dp,其中dp[i][j]表示前i个物品中选择若干个物品放入容量为j的背包时的最大价值。 根据多重背包问题的特点,我们需要对每个物品的个数进行遍历,并依次判断放入背包的个数是否超过c。 具体的状态转移方程为: dp[i][j] = max(dp[i-1][j-k*w[i]] + k*v[i]),其中0 <= k <= min(c[i], j/w[i]) 最后,需要注意的是多重背包问题的时间复杂度较高,为O(N*V*∑(c[i])),其中N是物品的数量,V是背包的容量,∑(c[i])表示物品的个数限制的总和。 总结而言,多重背包问题是在0-1背包问题和完全背包问题基础上的一种更复杂的情况,需要对每个物品的个数进行遍历和判断,采用动态规划求解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值