题目链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29608#problem/D
我把它化成了0-1背包,应该可以直接用多重背包做的,但是没有写好,所以一直算不对
贴代码:
1 #include<cstdio> 2 #include<cstring> 3 #define INF 0x3f3f3f3f 4 #define N 1005 5 int f[N],s[N]; 6 struct bake 7 { 8 int val,we; 9 } p[12*N]; 10 int main() 11 { 12 // freopen("in.c","r",stdin); 13 int n,m,we,val,cnt=0; 14 scanf("%d%d%d%d",&n,&m,&we,&val); 15 int num = n/we; 16 for(int i=1; i<=num; ++i) 17 p[cnt].val = val,p[cnt].we = we,++cnt; 18 // printf("p[%d].num = %d ",0,p[0].num); 19 for(int i=0; i<m; ++i) 20 { 21 int a,b; 22 scanf("%d%d%d%d",&a,&b,&we,&val); 23 num = a/b; 24 for(int j=1; j<=num; ++j) 25 p[cnt].val = val,p[cnt].we = we,++cnt; 26 // printf("p[%d].num = %d ",i,p[i].val); 27 } 28 f[0] =0; 29 for(int i=0; i<cnt; ++i) 30 for(int j=n; j>=p[i].we; --j) 31 if(f[j-p[i].we] + p[i].val > f[j] ) f[j] = f[j-p[i].we] + p[i].val ; 32 printf("%d ",f[n]); 33 return 0; 34 }