01背包:每样东西只能选一个
模板:滚动数组优化
#include using namespace std; const int N = 1010; int v[N], w[N]; // 存第i个物品的体积和价值 int n, m; int f[N]; // f存状态,行表示物品, 列表示背包大小 int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> v[i] >> w[i]; for (int i = 1; i <= n; i++) // i枚举物品 for (int j = m; j >= v[i]; j -- ) // j枚举空间 { f[j] = max(f[j], f[j - v[i]] + w[i]); } cout << f[m] << endl; return 0; }多重背包:每样东西能选s[i]个
模板:
#include using namespace std; const int N = 110; int n, m; int f[N]; int main() { cin >> n >> m; for(int i = 1; i <= n; i ++ ) { int v, w, s; cin >> v >> w >> s; for(int j = m; j >= 0; j -- ) { for(int k = 0; k <= s && k * v <= j; k ++ ) { f[j] = max(f[j], f[j - k * v] + k * w); } } } cout << f[m] << endl; return 0; }二进制优化:实际上是分堆,减少了i枚举数
#include using namespace std; const int N = 2010, M = 12010; int n, m, cnt = 1; int f[N]; int v[M], w[M]; int main() { cin >> n >> m; //二进制堆 for(int i = 1; i <= n; i ++ ) { int a, b, s, k = 1; cin >> a >> b >> s; while(k <= s) { s -= k; v[cnt] = k * a; w[cnt ++ ] = k * b; k *= 2; } if(s > 0) { v[cnt] = s * a; w[cnt ++ ] = s * b; } } 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; }完全背包:每样东西能选无限个
模板:完全背包的的优化就在于推公式,由于是无限个,所以就可以同加来消元
#include using namespace std; const int N = 1010; int v[N], w[N], f[N][N]; int n, m; int main() { cin >> n >> m; for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i]; /* 这个状态转移公式是这么推出来的 f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i], f[i - 1][j - 2 * v[i]] + 2 * w[i], ...) f[i][j - v[i]] = max(f[i - 1][j - v[i]], f[i - 1][j - 2 * v[i]] + w[i], f[i - 1][j - 3 * v[i]] + 2 * w[i], ...); */ for(int i = 1; i <= n; i ++ ) for(int j = 1; j <= m; j ++ ) { f[i][j] = f[i - 1][j]; if(v[i] <= j) f[i][j] = max(f[i - 1][j], f[i][j - v[i]] + w[i]); } cout << f[n][m] << endl; return 0; }
例题:
01背包变式,求其选择方法种数。这里我选择f[j] = f[j] + f[j - x] 选择它的种数+不选择它的种数
- #include
- using namespace std;
-
- const int N = 1e4 + 10;
- int n, m;
- int f[N];
- int main()
- {
- cin >> n >> m;
- f[0] = 1;
- for(int i = 1; i <= n; i ++ )
- {
- int x;
- cin >> x;
- for(int j = m; j >= x; j -- )
- {
- f[j] += f[j - x];
- }
- }
-
- cout << f[m];
-
- return 0;
- }
也是个01背包问题,只不过我将f[]用于枚举此时能够装的体积了。
- #include
- using namespace std;
-
- const int N = 1e5 + 10;
- int n, m;
- int f[N];
- int main()
- {
- cin >> m >> n;
-
- for(int i = 1; i <= n; i ++ )
- {
- int x;
- cin >> x;
-
- for(int j = m; j >= x; j -- )
- {
- f[j] = max(f[j], f[j - x] + x);
- }
- }
-
- cout << m - f[m];
-
- return 0;
- }
[USACO1.5] [IOI1994]数字三角形 Number Triangles - 洛谷
这是个递推题,杨辉三角,该位置的最大路径值=左上+正上
- #include
-
- using namespace std;
-
- const int N = 1010;
-
- int n;
-
- int a[N][N], f[N][N];
-
- int main()
-
- {
-
- cin >> n;
-
- for (int i = 1; i <= n; i++)
-
- for (int j = 1; j <= i; j++)
-
- cin >> a[i][j];
-
- f[1][1] = a[1][1];
-
- for (int i = 2; i <= n; i++)
-
- {
-
- for (int j = 1; j <= i; j++)
-
- {
-
- f[i][j] = a[i][j] + max(f[i - 1][j - 1], f[i - 1][j]);
- }
- }
-
- int ans = 0;
-
- for (int i = 1; i <= n; i++)
-
- {
-
- ans = max(ans, f[n][i]);
- }
-
- cout << ans << endl;
-
- return 0;
- }
经典01背包,板子题
经典完全背包,板子题
这个题我是用有向图遍历过的,每次找一条挖地雷的路,找到最大路保存下来。
- #include
- #include
- using namespace std;
-
- const int N = 40, M = 2 * N;
- int n;
- int w[N];
- int e[M], ne[M], h[N], idx;
- bool st[N];
- int res, ans, stp;
- int path[N], rpath[N];
-
- void add(int a, int b)
- {
- e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
- }
-
- bool check(int x)
- {
-
- if(ne[ne[h[x]]] == -1 || h[x] == -1)
- return true;
- }
-
- void dfs(int u, int cnt)
- {
- // st[u] = true;
- // cout << u << ' ';
- res += w[u];
- path[cnt] = u;
- if(check(u))
- {
- if(ans < res)
- {
- ans = res;
- stp = cnt;
- for(int i = 0; i <= cnt; i ++ )
- {
- rpath[i] = path[i];
- }
- }
- }
-
- for(int i = h[u]; i != -1; i = ne[i])
- {
- int j = e[i];
- int value = w[j];
- // if(!st[j])
- // {
- dfs(j, cnt + 1);
- // }
- }
- res -= w[u];
- }
-
- int main()
- {
- cin >> n;
-
- memset(h, -1, sizeof h);
- for(int i = 1; i <= n; i ++ )
- {
- cin >> w[i];
- }
-
- for(int i = 1; i < n; i ++ )
- {
- for(int j = i + 1; j <= n; j ++ )
- {
- int t;
- cin >> t;
- if(t) add(i, j);
- }
- }
-
- for(int i = 1; i <= n; i ++ ) dfs(i, 0);
-
- for(int i = 0; i <= stp; i ++ ) cout << rpath[i] << ' ';
- cout << endl;
- cout << ans << endl;
- return 0;
- }