poj 1276 多重背包

时间:2024-12-13 18:03:56
735 3  4 125  6 5  3 350    //735的最大额,3种,4个125,6个5,3个350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
735
630
0
0
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
int n,m,t;
int dp[maxn],w[maxn],v[maxn];
int nValue;
//0-1背包,代价为cost,获得的价值为weight
void ZeroOnePack(int cost,int weight)
{
for(int i=nValue;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //完全背包,代价为cost,获得的价值为weight
void CompletePack(int cost,int weight)
{
for(int i=cost;i<=nValue;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
} //多重背包
void MultiplePack(int cost,int weight,int amount)
{
if(cost*amount>=nValue) CompletePack(cost,weight);
else
{
int k=;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=;
}
ZeroOnePack(amount*cost,amount*weight);//这个不要忘记了,经常掉了
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d%d",&nValue,&n)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%d%d",&w[i],&v[i]);
}
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
MultiplePack(v[i],v[i],w[i]);
}
printf("%d\n",dp[nValue]);
}
}