• 编程算法


    全然背包问题 代码(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
    ", 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
    ", 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
    ", 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
    ", 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
    ", res);
    	}
    };
    
    
    int main(void)
    {
    	Program P;
    	P.solve();
        return 0;
    }
    
    
    

    输出:

    result = 7
    








  • 相关阅读:
    beanshell-接口返回结果与预期结果比较(预期结果为两个接口差集)-两个set集合的比较
    jmeter断言之Beanshell断言(判断数据库结果是否符合预期)
    beanshell查询结果多条取满足条件的一条数据作为前置步骤给其他接口参数使用
    beanshell判断响应数据是jsonobject类型还是jsonarray类型
    yarn global add安装的目录(window10)
    React 使用axios,将axios注入到全局使用
    解决H5支付宝支付空白页问题
    React Ant Design Mobile ListView 上拉刷新,下拉加载
    vue点击实现复制
    element 设置table表头样式
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7356831.html
Copyright © 2020-2023  润新知