http://acm.hdu.edu.cn/showproblem.php?pid=1171
多重背包
我的代码
1 #include <stdio.h>
2 #include <string.h>
3 const int N=250010;
4 int f[N],v[60],c[60];
5 int main()
6 {
7 int n,i,j,k,s,ss,cc;
8 while (scanf("%d",&n) && n>=0)
9 {
10 memset(f,0,sizeof(f)); f[0]=1;
11 ss=0;
12 for (i=1;i<=n;i++)
13 {
14 scanf("%d%d",&v[i],&c[i]);
15 ss+=v[i]*c[i];
16 }
17 s=ss/2;
18 for (i=1;i<=n;i++)
19 {
20 cc=c[i];
21 for (k=1;k<cc;cc-=k,k=k<<1)
22 for (j=s;j>=k*v[i];j--)
23 f[j]|=f[j-k*v[i]];
24 k=cc;
25 for (j=s;j>=k*v[i];j--)
26 f[j]|=f[j-k*v[i]];
27 }
28 while (!f[s]) s--;
29 printf("%d %d\n",ss-s,s);
30 }
31 }