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

来源:互联网 发布:wampserver mysql密码 编辑:程序博客网 时间:2024/06/05 15:23

贴一个自认为讲解不错的链接: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 231).

Sample Input

15 101 2 3 4 55 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

310 11021 130 5010 11021 150 301 6210 320 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 501int 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 101int 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;}


0 0
原创粉丝点击