赞
踩
回顾一下 01 背包问题 (每个物品只能选一次)
集合划分的依据:用“最后一步”来划分
完全背包问题:每个物品可以选 0,1,2,3... 个
通过比较可以得到:
背包问题的注意事项
- #include <cstring>
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- const int N = 20010;
-
- int n, m;
- int f[N], g[N], q[N];
-
- int main()
- {
- cin >> n >> m;
- for (int i = 0; i < n; i ++ )
- {
- int v, w, s;
- cin >> v >> w >> s;
- memcpy(g, f, sizeof f);
- for (int j = 0; j < v; j ++ )
- {
- int hh = 0, tt = -1;
- for (int k = j; k <= m; k += v)
- {
- if (hh <= tt && q[hh] < k - s * v) hh ++ ;
- while (hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt -- ;
- q[ ++ tt] = k;
- f[k] = g[q[hh]] + (k - q[hh]) / v * w;
- }
- }
- }
-
- cout << f[m] << endl;
-
- return 0;
- }
70 3 71 100 69 1 1 2输出样例:
3
这是01背包问题的简单应用
- #include <iostream>
-
- using namespace std;
-
-
- const int N = 1010;
-
- int n, m;
- int f[N];
-
- int main()
- {
- cin >> m >> n;
- for(int i = 0; i < n; i ++ )
- {
- int v, w;
- cin >> v >> w;
- for(int j = m; j >= v; j -- ) f[j] = max(f[j], f[j - v] + w);
- }
-
- cout << f[m] << endl;
-
- return 0;
- }
输入样例:
24 6 8 3 12 7 9 7输出样例:
0
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- const int N = 200010;
-
- int m, n;
- int f[N];
-
- int main()
- {
- cin >> m >> n;
- for(int i = 0; i < n; i ++ )
- {
- int v;
- cin >> v;
- for(int j = m; j >= v; j --) f[j] = max(f[j], f[j - v] + v);
- }
-
- cout << m - f[m] << endl;
- return 0;
- }
输入样例1:
10 100 5 7 10 2 40 2 50 1 20 4 20输出样例1:
3 30
输入样例2:
10 100 5 8 110 12 10 20 10 5 200 1 110输出样例2:
0 100
状态计算:
最多收服的小精灵数量:
最小消耗的体力:
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- const int N = 1010, M = 510;
-
- int n, V1, V2;
- int f[N][M];
-
- int main()
- {
- cin >> V1 >> V2 >> n;
- for(int i = 0; i < n; i ++ )
- {
- int v1, v2;
- cin >> v1 >> v2;
- for(int j = V1; j >= v1; j -- )
- for(int k = V2; k >= v2; k --)
- f[j][k] = max(f[j][k], f[j - v1][k - v2] + 1);
- }
-
- cout << f[V1][V2 - 1] << ' ';
- int k = V2 - 1;
- while(k > 0 && f[V1][k - 1] == f[V1][V2 - 1]) k --;
- cout << V2 - k << endl;
-
- return 0;
- }
输入样例
4 5 6 1 2 3 2 4 4 3 4 5 4 5 6输出样例:
8
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- const int N = 110;
-
- int n, V, M;
- int f[N][N];
-
- int main()
- {
- cin >> n >> V >> M;
- for(int i = 0; i < n; i ++ )
- {
- int v, m, w;
- cin >> v >> m >> w;
- for(int j = V; j >= v; j -- )
- for(int k = M; k >= m; k -- )
- f[j][k] = max(f[j][k], f[j - v][k - m] + w);
- }
-
- cout << f[V][M] << endl;
-
- return 0;
- }
输入样例:
5 60 5 3 36 120 10 25 129 5 50 250 1 45 130 4 20 119输出样例:
249
- #include <cstring>
- #include <iostream>
-
- using namespace std;
-
- const int N = 22, M = 80;
-
- int n, m, K;
- int f[N][M];
-
- int main()
- {
- cin >> n >> m >> K;
- memset(f, 0x3f, sizeof f);
- f[0][0] = 0;
-
- while (K -- )
- {
- int v1, v2, w;
- cin >> v1 >> v2 >> w;
- for (int i = n; i >= 0; i -- )
- for (int j = m; j >= 0; j -- )
- f[i][j] = min(f[i][j], f[max(0, i - v1)][max(0, j - v2)] + w);
- }
-
-
- cout << f[n][m] << endl;
-
- return 0;
- }
输入样例:
4 4 1 1 2 2输出样例:
3
将 M 看成是背包容量
每个数看成是一个物品,Ai 看成是体积
目标:求出总体积恰好是 M 的方案数
- #include <iostream>
-
- using namespace std;
-
- const int N = 100010;
-
- int n, m;
- int f[N];
-
- int main()
- {
- cin >> n >> m;
- f[0] = 1;
-
- for(int i = 0; i < n; i ++ )
- {
- int v;
- cin >> v;
- for(int j = m; j >= v; j -- )
- f[j] += f[j - v];
- }
-
- cout << f[m] << endl;
-
- return 0;
- }
输入样例:
5 1000 80 20 4 40 50 9 30 50 7 40 30 6 20 20 1输出样例:
1040
多重背包问题的模板题
- #include <iostream>
-
- using namespace std;
-
- const int N = 6010;
-
- int n, m;
- int f[N];
-
- int main()
- {
- cin >> n >> m;
- for(int i = 0; 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;
- }
输入样例1:
20
输出样例1:
2
输入样例2:
15
输出样例2:
0
输入样例3:
0
输出样例3:
1
二维DP的朴素写法
- #include <iostream>
-
- using namespace std;
-
- const int N = 1010;
-
- int v[5] = {0, 10, 20, 50, 100};
- int f[5][N];
-
- int main()
- {
- int m;
- cin >> m;
-
- f[0][0] = 1;
- for(int i = 1; i <= 4; i ++ )
- for(int j = 0; j <= m; j ++ )
- {
- f[i][j] = f[i - 1][j];
- if(j >= v[i]) f[i][j] += f[i][j - v[i]];
- }
-
- cout << f[4][m] << endl;
-
- return 0;
- }
优化后的一维DP解法
- #include <iostream>
-
- using namespace std;
-
- const int N = 1010;
-
- int v[5] = {0, 10, 20, 50, 100};
- int f[N];
-
- int main()
- {
- int m;
- cin >> m;
-
- f[0] = 1;
- for(int i = 1; i <= 4; i ++ )
- for(int j = v[i]; j <= m; j ++ )
- {
- f[j] += f[j - v[i]];
- }
-
- cout << f[m] << endl;
-
- return 0;
- }
输入样例
4 5 1 2 2 4 3 4 4 6输出样例:
1 4
01背包问题的思路如下
其实是判断出每个物体是否被选
动态规划问题 -> 最短路问题(求路径问题)
- #include <iostream>
-
- using namespace std;
-
- const int N = 1010;
-
- int n, m;
- int v[N], w[N];
- int f[N][N];
-
- int main()
- {
- cin >> n >> m;
- for (int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
- for (int i = n; i >= 1; i -- )
- for (int j = 0; j <= m; j ++ )
- {
- f[i][j] = f[i + 1][j];
- if (j >= v[i]) f[i][j] = max(f[i][j], f[i + 1][j - v[i]] + w[i]);
- }
-
- int j = m;
- for (int i = 1; i <= n; i ++ )
- if (j >= v[i] && f[i][j] == f[i + 1][j - v[i]] + w[i])
- {
- cout << i << ' ';
- j -= v[i];
- }
-
- return 0;
- }
输入样例:
3 3 30 40 50 20 30 50 20 25 30输出样例:
70 1 1 2 1 3 1
这道题的本质上是一个分组背包问题+背包问题求具体方案
- #include <iostream>
- #include <algorithm>
-
- using namespace std;
-
- const int N = 11, M = 16;
-
- int n, m;
- int w[N][M];
- int f[N][M];
- int way[N];
-
- int main()
- {
- cin >> n >> m;
- for (int i = 1; i <= n; i ++ )
- for (int j = 1; j <= m; j ++ )
- cin >> w[i][j];
- for (int i = 1; i <= n; i ++ )
- for (int j = 0; j <= m; j ++ )
- for (int k = 0; k <= j; k ++ )
- f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);
-
- cout << f[n][m] << endl;
-
- int j = m;
- for (int i = n; i; i -- )
- for (int k = 0; k <= j; k ++ )
- if (f[i][j] == f[i - 1][j - k] + w[i][k])
- {
- way[i] = k;
- j -= k;
- break;
- }
-
- for (int i = 1; i <= n; i ++ ) cout << i << ' ' << way[i] << endl;
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。