Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 9768 Accepted
Submission(s): 4911
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 const int maxn=10005; 5 const int inf=-0x3f3f3f3f; 6 int dp[maxn],coin[501],weight[501]; 7 int main() 8 { 9 int i,j,n; 10 int test,low,high,cnt; 11 scanf("%d",&test); 12 while(test--) 13 { 14 scanf("%d%d",&low,&high); 15 cnt=high-low; 16 scanf("%d",&n); 17 for(i=0;i<n;i++) 18 scanf("%d%d",&coin[i],&weight[i]); 19 /*for(i=1;i<maxn;i++) 20 dp[i]=inf;*/ 21 memset(dp,-1,sizeof(dp)); 22 dp[0]=0; 23 for(i=0;i<n;i++) 24 { 25 for(j=weight[i];j<=cnt;j++) 26 { 27 if(dp[j-weight[i]]>-1&&(dp[j]==-1||dp[j]>(dp[j-weight[i]]+coin[i]))) 28 dp[j]=dp[j-weight[i]]+coin[i]; 29 } 30 } 31 if(dp[cnt]==-1) 32 printf("This is impossible.\n"); 33 else 34 printf("The minimum amount of money in the piggy-bank is %d.\n",dp[cnt]); 35 } 36 return 0; 37 }
HDUOJ---Piggy-Bank,布布扣,bubuko.com
原文:http://www.cnblogs.com/gongxijun/p/3584367.html