很标准的01背包问题
1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen("2602in.txt", "r", stdin); 14 #endif 15 16 int T; 17 scanf("%d", &T); 18 while(T--) 19 { 20 int n, V; 21 scanf("%d%d", &n, &V); 22 for(int i = 0; i < n; ++i) 23 scanf("%d", &w[i]); 24 for(int i = 0; i < n; ++i) 25 scanf("%d", &v[i]); 26 memset(dp, 0, sizeof(dp)); 27 for(int i = 0; i < n; ++i) 28 for(int j = V; j >= v[i]; --j) 29 dp[j] = max(dp[j], dp[j-v[i]] + w[i]); 30 printf("%d\n", dp[V]); 31 } 32 return 0; 33 }
代码君
HDU 2602 (简单的01背包) Bone Collector
时间: 2024-12-26 09:55:43