0-1背包:
Link:http://acm.hdu.edu.cn/showproblem.php?pid=2602
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32017 Accepted Submission(s): 13180
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
Author
Teddy
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int w[1001],v[1001],N,V,f[1001];
int main()
{
int i,j,T;
while(scanf("%d",&T)==1)
{
while(T--)
{
scanf("%d%d",&N,&V);
for(i=1;i<=N;i++)
{
scanf("%d",&w[i]);
}
for(i=1;i<=N;i++)
{
scanf("%d",&v[i]);
}
for(i=0;i<=V;i++)
f[i]=0;
for(i=1;i<=N;i++)
{
for(j=V;j>=v[i];j--)
{
f[j]=max(f[j],f[j-v[i]]+w[i]);
}
}
printf("%d\n",f[V]);
}
}
return 0;
}
完全背包:
Link:http://acm.hdu.edu.cn/showproblem.php?pid=1114
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12691 Accepted Submission(s): 6422
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.
Source
Central Europe 1999
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int w[1001],v[1001],N,V,f[10001];
int main()
{
int e,i,j,T;
while(scanf("%d",&T)==1)
{
while(T--)
{
scanf("%d%d%d",&e,&V,&N);
V-=e;
for(i=1;i<=N;i++)
{
scanf("%d%d",&w[i],&v[i]);
}
f[0]=0;
for(i=1;i<=V;i++)
f[i]=99999999;
for(i=1;i<=N;i++)
{
for(j=v[i];j<=V;j++)
{
f[j]=min(f[j],f[j-v[i]]+w[i]);
}
}
if(f[V]==99999999)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",f[V]);
}
}
return 0;
}
多重背包:
Link:http://acm.hdu.edu.cn/showproblem.php?pid=2191
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15623 Accepted Submission(s): 6623
Problem Description
急!灾区的食物依然短缺!
为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。
请问:你用有限的资金最多能采购多少公斤粮食呢?
后记:
人生是一个充满了变数的生命过程,天灾、人祸、病痛是我们生命历程中不可预知的威胁。
月有阴晴圆缺,人有旦夕祸福,未来对于我们而言是一个未知数。那么,我们要做的就应该是珍惜现在,感恩生活——
感谢父母,他们给予我们生命,抚养我们成人;
感谢老师,他们授给我们知识,教我们做人
感谢朋友,他们让我们感受到世界的温暖;
感谢对手,他们令我们不断进取、努力。
同样,我们也要感谢痛苦与艰辛带给我们的财富~
Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。
Output
对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。
Sample Input
Sample Output
Author
lcy
Source
2008-06-18《 ACM程序设计》期末考试——四川加油!中国加油!
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int N;//物品个数
int V;//背包容量
int Weight[111];
int Value[111];
int Num[111];
int f[111];
/*
f[v]:表示把前i件物品放入容量为v的背包中获得的最大收益。
f[v] = max(f[v],f[v - Weight[i]] + Value[i]);
v的为逆序
*/
void ZeroOnePack(int nWeight,int nValue)
{
for (int v = V;v >= nWeight;v--)
{
f[v] = max(f[v],f[v - nWeight] + nValue);
}
}
/*
f[v]:表示把前i件物品放入容量为v的背包中获得的最大收益。
f[v] = max(f[v],f[v - Weight[i]] + Value[i]);
v的为增序
*/
void CompletePack(int nWeight,int nValue)
{
for (int v = nWeight;v <= V;v++)
{
f[v] = max(f[v],f[v - nWeight] + nValue);
}
}
int MultiKnapsack()
{
int k = 1;
int nCount = 0;
for (int i = 1;i <= N;i++)
{
if (Weight[i] * Num[i] >= V)
{
//完全背包:该类物品原则上是无限供应,
//此时满足条件Weight[i] * Num[i] >= V时,
//表示无限量供应,直到背包放不下为止.
CompletePack(Weight[i],Value[i]);
}
else
{
k = 1;
nCount = Num[i];
while(k <= nCount)
{
ZeroOnePack(k * Weight[i],k * Value[i]);
nCount -= k;
k *= 2;
}
ZeroOnePack(nCount * Weight[i],nCount * Value[i]);
}
}
return f[V];
}
int main()
{
int T,i,ans;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d",&V,&N);
for(i=1;i<=N;i++)
scanf("%d%d%d",&Weight[i],&Value[i],&Num[i]);
memset(f,0,sizeof(f));
ans=MultiKnapsack();
cout<<ans<<endl;
}
}
return 0;
}