链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
这道题是要找最小的可能值,所以初始化的时候要注意一下不同。完全背包的题,要注意总结这些模版题
View Code
1 #include<stdio.h> 2 #include<string.h> 3 #define MAXA 1000000000 4 #define N 10005 5 #define M 505 6 int a[N]; 7 int wei[M]; 8 int val[M]; 9 int min(int a,int b) 10 { 11 return a<b?a:b; 12 } 13 int main() 14 { 15 int n,t,p,w,e,f,m,i,j,k; 16 scanf("%d",&t); 17 while(t--) 18 { 19 scanf("%d%d",&e,&f); 20 m=f-e; 21 scanf("%d",&n); 22 for(i=0;i<=m;i++) 23 a[i]=MAXA; 24 for(i=0;i<n;i++) 25 scanf("%d%d",&val[i],&wei[i]); 26 a[0]=0; 27 for(i=0;i<n;i++) 28 for(j=wei[i];j<=m;j++) 29 a[j]=min(a[j],a[j-wei[i]]+val[i]); 30 if(a[m]==MAXA) 31 printf("This is impossible.\n"); 32 else 33 printf("The minimum amount of money in the piggy-bank is %d.\n",a[m]); 34 } 35 return 0; 36 }