背包问题模板
2016-08-07 20:28
211 查看
/* 背包问题模板 【若要求恰好装满,初始化时f[1...V] = -1(求最大)而f[0] = 0】 */ #include <iostream> #include <algorithm> using namespace std; const int M = 1000; int c[M], w[M], nl[M];//c:费用 w:价值 n1:数量 int f[M];//f[与V有关],c和w[与n]有关 int v, V, V1;//V:容量 V1:容量2 //01背包 void ZeroOnePack(int c, int w){ for (int v = V; v >= c; v--) f[v] = max(f[v], f[v - c] + w); } //完全背包 void CompletePack(int c, int w){ for (int v = c; v <= V; v++) f[v] = max(f[v], f[v - c] + w); } //多重背包 void MultiplePack(int c, int w, int n1){ if (c * n1 >= V) CompletePack(c, w); else{ int k = 1; while (k < n1){ //二进制拆分 ZeroOnePack(k*c, k*w); n1 -= k; k <<= 1; } ZeroOnePack(n1*c, n1*w); } }
相关文章推荐
- 多重背包问题(含模板)
- 模板①:背包问题(0-1背包&完全背包&多重背包)
- 背包问题模板(01背包,完全背包,多重背包)
- HDU 1114 Piggy-Bank(完全背包问题)【完全背包--刚好装满 模板】
- 背包问题模板
- 部分背包问题模板
- Python基于回溯法子集树模板解决0-1背包问题实例
- 背包问题_模板
- 背包问题的三个模板
- POJ 1014 Dividing (多重背包问题+递归)【模板】
- 背包问题模板
- 背包问题标准模板
- nyoj106背包问题为模板解出杭电oj2187悼念512汶川大地震遇难同胞——老人是真饿了
- 背包问题模板
- 二维背包问题 + 代码模板
- 背包九讲问题的模板
- 简单背包问题模板
- ACM_模板_背包问题
- 总结——背包问题解析及模板代码
- 算法模板之01背包问题