http://acm.hdu.edu.cn/showproblem.php?pid=1114
初始化为无穷大
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #define INF 0xfffffff 5 using namespace std; 6 int main() 7 { 8 int i,j,k,n,t,f[10001],v,p[501],w[501]; 9 scanf("%d", &t); 10 while(t--) 11 { 12 scanf("%d%d",&n,&v); 13 v = v-n; 14 scanf("%d",&n); 15 for(i = 1; i <= n; i++) 16 scanf("%d%d",&p[i],&w[i]); 17 for(i = 1 ; i <= v; i++) 18 f[i] = INF; 19 f[0] = 0; 20 for(i =1; i <= n; i++) 21 { 22 for(j = w[i] ; j <= v; j++)//shu xu 23 if(f[j]>f[j-w[i]]+p[i]) 24 f[j] = f[j-w[i]]+p[i]; 25 } 26 if(f[v]==INF) 27 printf("This is impossible.\n"); 28 else 29 printf("The minimum amount of money in the piggy-bank is %d.\n",f[v]); 30 } 31 return 0; 32 }