标签:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 const int max_size = 1000000 + 10; 8 const int MAX = 105; 9 int dp[max_size]; 10 bool vis[max_size]; 11 12 13 14 int main() 15 { 16 //1.将问题的模型抽象出来 17 int cnt, vol; 18 int val[MAX]; 19 int num[MAX]; 20 while(scanf("%d %d", &cnt, &vol) != EOF) 21 { 22 memset(dp, 0, sizeof(dp)); 23 memset(vis, false, sizeof(vis)); 24 if(cnt == 0 && vol == 0) 25 break; 26 for(int i = 0; i < cnt; i++) 27 scanf("%d", val+i); 28 for(int i = 0; i < cnt; i++) 29 scanf("%d", num+i); 30 31 for(int i = 0; i < cnt; i++) 32 { 33 if(val[i] * num[i] >= vol) 34 { 35 //CompletePack(val[i], val[i]); //那么多的价值,那么多的占用? 36 37 for(int j = val[i]; j <= vol; j++) ///多重背包这里错了两次了,要注意,昨天找了一晚上 38 { 39 dp[j] = max(dp[j], dp[j - val[i]] + val[i]); 40 vis[dp[j]] = true; 41 } 42 continue; 43 } 44 int k = 1; 45 while(k < num[i]) 46 { 47 //ZeroOnePack(k*val[i], k*val[i]); 48 for(int j = vol; j - k * val[i] >= 0; j--) 49 { 50 dp[j] = max(dp[j], dp[j-k*val[i]] + k*val[i]); 51 vis[dp[j]] = true; 52 } 53 num[i] -= k; 54 k *= 2; 55 } 56 //ZeroOnePack(num[i]*val[i], num[i]*val[i]); 57 for(int j = vol; j - num[i]*val[i] >= 0; j--) 58 { 59 dp[j] = max(dp[j], dp[j - num[i]*val[i]] + num[i]*val[i]); 60 vis[dp[j]] = true; 61 } 62 } 63 64 int ans = 0; 65 for(int i = 1; i <= vol; i++) 66 { 67 ans += (vis[i] == true) ? 1 : 0; 68 } 69 printf("%d\n", ans); 70 } 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/ya-cpp/p/4337701.html