多重背包

来源:互联网 发布:java培训班北京 编辑:程序博客网 时间:2024/04/29 10:38
/*时间复杂度 O(VN) */#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;#define maxx 100010int dp[maxx];int ZeroOnepack(int cost ,int worth ,int v ){for(int i=v;i>=cost;i--)dp[i]=max(dp[i],dp[i-cost]+worth);}int CompletePack(int cost,int worth,int v){for(int i=cost;i<=v;i++)dp[i]=max(dp[i],dp[i-cost]+worth);}int MuiltplePack(int cost,int worth,int v,int m){if(cost*m>=v)CompletePack(cost,worth,v);else{int k=1;while(k<m){ZeroOnepack(k*cost,k*worth,v);m=m-k;k=k*2;}ZeroOnepack(m*cost,m*worth,v);}}int main(){int T,n,m,p[103],h[103],c[103];cin>>T;while(T--){scanf("%d%d",&n,&m);for(int i=0;i<m;i++)scanf("%d%d%d",&p[i],&h[i],&c[i]);memset(dp,0,sizeof(dp));for(int i=0;i<m;i++)MuiltplePack(p[i],h[i],n,c[i]);cout<<dp[n]<<endl;}return 0;}


原创粉丝点击