标签:
1 int dp[105][10005]; 2 3 int v[105]; 4 int w[105]; 5 6 int main(){ 7 8 int n,W; 9 while(scanf("%d",&n) != EOF && n){ 10 for(int i = 0 ; i < n ; i ++){ 11 scanf("%d%d",&w[i],&v[i]); 12 } 13 scanf("%d",&W); 14 15 memset(dp[0],0x7f7f7f7f,sizeof(dp[0])); 16 dp[0][0] = 0; 17 /* 18 dp[i][j] 表示 选取 i 件物品, 价值为 j 时的最小重量 19 */ 20 for(int i = 0 ; i < n ; i ++){ 21 for(int j = 0 ; j <= 100*100 ; j ++){ 22 if(j < v[i]){ 23 dp[i+1][j] = dp[i][j]; 24 }else{ 25 dp[i+1][j] = min(dp[i][j],dp[i][j-v[i]] + w[i]); 26 } 27 } 28 } 29 30 int ans = 0; 31 for(int i = 0 ; i <= 10000 ; i ++){ 32 if(dp[n][i] <= W) ans = i; 33 } 34 printf("%d\n",ans); 35 36 } 37 return 0; 38 } 39 /* 40 4 41 2 3 42 1 2 43 3 4 44 5 45 46 7 47 */
标签:
原文地址:http://www.cnblogs.com/zstu-jack/p/5324112.html