Background: WA a few times, are small mistakes: put i--written i++ and the like, when writing must think of specific intentions. And you must write at least three sets of test data!!!!!!!
Learning: Templating to write multiple backpacks.
#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int t,v,n;int c[109],w[ 109],num[109],f[109];void zeroonebag (int cost,int weight) {for (int i=v;i >= cost;i--) F[i]=max (F[i],f[i-cost]+weigh t);} void completebag (int cost,int weight) {for (int i=cost;i <= v;i++) F[i]=max (f[i],f[i-cost]+weight);} void multiplybag (int cost,int weight,int num) {if (Cost*num >= v) completebag (cost,weight); else{int k=1,m=num; while (K < m) {zeroonebag (k*cost,k*weight); M-=k; k*=2; } zeroonebag (M*cost,m*weight); }}int Main (void) {scanf ("%d", &t); while (t--) {scanf ("%d%d", &v,&n); for (int i=0;i < n;i++) scanf ("%d%d%d", &c[i],&w[i],&num[i]); memset (f,0,sizeof (F)); for (int i=0;i < n;i++) multiplybag (C[i],w[i],num[i]); printf ("%d\n", F[v]); } return 0;}
HDU 2,191 Heavy Backpack, write your own template