HDU 1114完全背包
简化的动态规划方程:
for i=1..N
for v=0..V
f[v]=max{f[v],f[v-c[i]]+w[i]}
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define m 200000000 5 6 int main() 7 { 8 int i,j,v,ncases,f[10005]; 9 int empty,full,V,N,P[505],W[505]; 10 11 scanf("%d",&ncases); 12 while( ncases-- ) 13 { 14 scanf("%d%d",&empty,&full); 15 V = full-empty; 16 scanf("%d",&N); 17 18 for(i=1; i<=N; i++) 19 scanf("%d%d",&P[i],&W[i]); 20 for(i=0; i<=V; i++) 21 f[i] = m; 22 23 f[0] = 0; 24 for(i=1; i<=N; i++) 25 { 26 for(j=W[i]; j<=V; j++) 27 { 28 if(f[j] > f[j-W[i]]+P[i]) 29 f[j] = f[j-W[i]]+P[i]; 30 } 31 } 32 33 if(f[V] == m) 34 printf("This is impossible.\n"); 35 else 36 printf("The minimum amount of money in the piggy-bank is %d.\n",f[V]); 37 38 } 39 system("pause"); 40 return 0; 41 }