全然背包问题 代码(C)
本文地址: http://blog.csdn.net/caroline_wendy
题目: 有n个重量和价值分别为w,v的物品, 从这些物品中挑选出总重量不超过W的物品, 求全部挑选方案中价值总和的最大值.
*每件物品能够挑选随意多件.
动态规划: 每次选取最大的组合, 增加到数组, 第一种时间复杂度O(nW^2), 另外一种时间复杂度O(nW).
解法1, max()部分表明, 要么来源于上面, 要么来源于前面.
代码:
/* * main.cpp * * Created on: 2014.7.17 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm> using namespace std; class Program { static const int MAX_N = 100; int n=4, W=5; int w[MAX_N] = {2,1,3,2}, v[MAX_N]={3,2,4,2}; int dp[MAX_N+1][MAX_N+1]; public: void solve() { for (int i=0; i<n; ++i) { for (int j=0; j<=W; j++) { for (int k=0; k*w[i] <= j; k++) { dp[i+1][j] = max(dp[i+1][j], dp[i][j-k*w[i]]+k*v[i]); } } } printf("result = %d\n", dp[n][W]); } void solve1() { for (int i=0; i<n; ++i) { for (int j=0; j<=W; j++) { if (j<w[i]) { dp[i+1][j] = dp[i][j]; } else { dp[i+1][j] = max(dp[i][j], dp[i+1][j-w[i]]+v[i]); } } } printf("result = %d\n", dp[n][W]); } }; int main(void) { Program P; P.solve1(); return 0; }
输出:
result = 10
为了节约内存, 能够使用一维数组进行求解.
代码:
/* * main.cpp * * Created on: 2014.7.17 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm> using namespace std; class Program { static const int MAX_N = 100; int n=3, W=7; int w[MAX_N] = {3,4,2}, v[MAX_N]={4,5,3}; int dp[MAX_N+1]; public: void solve() { memset(dp, 0, sizeof(dp)); for (int i=0; i<n; ++i) { for (int j=w[i]; j<=W; j++) { dp[j] = max(dp[j], dp[j-w[i]]+v[i]); } } printf("result = %d\n", dp[W]); } }; int main(void) { Program P; P.solve(); return 0; }
输出:
result = 10
能够讲两个数组滚动使用, 节省内存.
代码:
/* * main.cpp * * Created on: 2014.7.17 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm> using namespace std; class Program { static const int MAX_N = 100; int n=3, W=7; int w[MAX_N] = {3,4,2}, v[MAX_N]={4,5,3}; int dp[2][MAX_N+1]; public: void solve() { memset(dp,0,sizeof(dp)); for (int i=0; i<n; ++i) { for (int j=0; j<=W; j++) { if (j<w[i]) { dp[(i+1)&1][j] = dp[i&1][j]; } else { dp[(i+1)&1][j] = max(dp[i&1][j], dp[(i+1)&1][j-w[i]]+v[i]); } } } printf("result = %d\n", dp[n&1][W]); } }; int main(void) { Program P; P.solve(); return 0; }
输出:
result = 10
当重量可选范围非常大, 价值可选范围小的时候, 能够使依据价值进行动态规划.
代码:
/* * main.cpp * * Created on: 2014.7.17 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <memory.h> #include <limits.h> #include <utility> #include <queue> #include <algorithm> using namespace std; class Program { static const int MAX_N = 100; static const int MAX_V = 4; const int INF = INT_MAX>>2; int n=4, W=5; int w[MAX_N] = {2,1,3,2}, v[MAX_N] = {3,2,4,2}; int dp[MAX_N+1][MAX_N*MAX_V+1]; public: void solve() { fill(dp[0], dp[0]+MAX_N*MAX_V+1, INF); dp[0][0] = 0; for (int i=0; i<n; ++i) { for (int j=0; j<=MAX_N*MAX_V; j++) { if (j<v[i]) { dp[i+1][j] = dp[i][j]; } else { dp[i+1][j] = min(dp[i][j], dp[i][j-v[i]]+w[i]); } } } int res = 0; for (int i=0; i<=MAX_N*MAX_V; ++i) if (dp[n][i]<=W) res = i; printf("result = %d\n", res); } }; int main(void) { Program P; P.solve(); return 0; }
输出:
result = 7