1. 程式人生 > >完全揹包-hdu1114

完全揹包-hdu1114

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1114

題目描述:

程式碼實現:

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int dp[1000005];
 5 int main()
 6 {
 7     int w[500],p[500];
 8     int T,n,E,F,weight,i,j;
 9     scanf("%d",&T);
10     while(T--){
11         scanf("%d%d
",&E,&F); 12 weight=F-E; 13 for(i=0;i<=weight;i++) 14 dp[i]=10000000; 15 scanf("%d",&n); 16 for(i=0;i<n;i++){ 17 scanf("%d%d",&p[i],&w[i]); 18 } 19 dp[0]=0; 20 for(i=0;i<n;i++){ 21 for(j=w[i];j<=weight;j++){
22 dp[j]=min(dp[j],dp[j-w[i]]+p[i]); 23 } 24 } 25 if(dp[weight]==10000000) 26 printf("This is impossible.\n"); 27 else 28 printf("The minimum amount of money in the piggy-bank is %d.\n",dp[weight]); 29 } 30 return 0; 31 }