一本通 1270:【例9.14】混合背包
作者:互联网
混合背包
混合背包模板题。
#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//
#define M 100000
int c[M], w[M];
int f[M], new_c[M], new_w[M], num[M];
bool it[M];
int v, m, tot;
int main() {
scanf("%d%d", &v, &m);
for(int i = 1; i <= m; i++) scanf("%d%d%d", &c[i], &w[i], &num[i]);
for(int i = 1; i <= m; i++) {
if(num[i] == 0) {
it[++tot] = 1;
new_c[tot] = c[i];
new_w[tot] = w[i];
continue;
}
if(num[i] == 1) {
new_c[++tot] = c[i];
new_w[tot] = w[i];
continue;
}
for(int j = 1; j <= num[i]; j<<=1) {
num[i] -= j;
new_c[++tot] = c[i] * j;
new_w[tot] = w[i] * j;
}
if(num[i]) {
new_c[++tot] = c[i] * num[i];
new_w[tot] = w[i] * num[i];
num[i] = 0;
}
}
for(int i = 1; i <= tot; i++) {
if(it[i]) {
for(int j = new_c[i]; j <= v; j++)
f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
}
else
for(int j = v; j >= new_c[i]; j--)
f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
}
printf("%d\n", f[v]);
return 0;
}
标签:背包,1270,9.14,混合,int,new,include 来源: https://www.cnblogs.com/Benjamin-cpp/p/10846655.html