标签:背包 std 多重 code 模板 bsp using mes turn
二进制优化
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cmath> using namespace std; const int maxn = 25000; const int maxm = 2005; int dp[maxn], vv, s[maxm], v1[maxm], w1[maxm]; int v2[maxn], w2[maxn]; int cnt, n; int main() { cin >> n >> vv; for(int i = 1; i <= n; i++) cin >> v1[i] >> w1[i] >> s[i]; for(int i = 1; i <= n; i++) { for(int j = 1; j <= s[i]; j <<= 1) { v2[++cnt] = j*v1[i]; w2[cnt] = j*w1[i]; s[i] -= j; } if(s[i]) { v2[++cnt] = s[i]*v1[i]; w2[cnt] = s[i]*w1[i]; } } for(int i = 1; i <= cnt; i++) for(int j = vv; j >= v2[i]; j--) dp[j] = max(dp[j], dp[j-v2[i]]+w2[i]); cout << dp[vv]; return 0; }
标签:背包 std 多重 code 模板 bsp using mes turn
原文地址:https://www.cnblogs.com/lovezxy520/p/11838446.html