第29行,ss是物品的个数。当ss==0时,是完全背包问题,此时将ss赋值为999999,转换为多重背包问题,进而通过二进制优化转化为01背包问题。
为什么当ss==0时将ss赋值为0x3f3f3f3f或INT_MAX会RE?
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
using namespace std;
// D
const int M = 30 * 60;
const int N = 1000010;//(1e4 + 10) * log2(110);
int n, m;
int cnt;
int v[N], w[N];
int f[M];
int main() {
int h1, m1, h2, m2;
scanf("%d:%d %d:%d %d", &h1, &m1, &h2, &m2, &n);
m = (h2 - h1) * 60 + m2 - m1;
for (int i = 1; i <= n; i ++) {
int vv, ww, ss; cin >> vv >> ww >> ss;
if (ss == 0) {
ss = 999999;//0x3f3f3f3f;
}
int k = 1;
while (ss >= k) {
cnt ++;
v[cnt] = k * vv;
w[cnt] = k * ww;
ss -= k;
k *= 2;
}
if (ss) {
cnt ++;
v[cnt] = ss * vv;
w[cnt] = ss * ww;
}
}
n = cnt;
for (int i = 1; i <= n; i ++) {
for (int j = m; j >= v[i]; j --) {
f[j] = max(f[j], f[j-v[i]] + w[i]);
}
}
cout << f[m] << endl;
return 0;
}