0-1背包问题的动态规划求解
来源:互联网 发布:台湾有几个网络 编辑:程序博客网 时间:2024/05/18 16:14
思路:
设value_get[i][j]为把前i个物品装入容量为j的背包所能获得的最大价值
需考虑能否装入物品i,以及是否装入物品i,递归式为:
value_get[i][j] = max{ value_get[i-1][j], value_get[i-1][ j-weight[i] ] + value[i] }
c语言代码如下:
/************************************************************************* > File Name: knapsack_dynamic.c > Author: arui > Mail: 450252722@qq.com > Created Time: Wed 26 Feb 2014 01:43:14 PM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>int knapsack_dynamic(int capacity, int *weight,int *value,int *choice, int obj_num){int ** value_get = (int**)malloc(sizeof(int*) * (obj_num + 1));for(int i = 0; i <= obj_num; ++i){value_get[i] = (int*)malloc(sizeof(int) * (capacity + 1));}//initializefor(int j = 0; j <= capacity; ++j){value_get[0][j] = 0;}for(int i = 0; i <= obj_num; ++i){value_get[i][0] = 0;}for(int i=1; i <= obj_num; ++i){for(int j=1; j <= capacity; ++j){value_get[i][j] = value_get[i-1][j];if(weight[i] <= j){int temp_value = value_get[i-1][j-weight[i]] + value[i];if(temp_value > value_get[i][j])value_get[i][j] = temp_value;}}}//construct the choiceint j = capacity;for(int i = obj_num; i > 0; --i){if(value_get[i][j] > value_get[i-1][j]){choice[i] = 1;j = j - weight[i];}}//free the space for(int i = 0; i <= obj_num; ++i){free(value_get[i]);}free(value_get);return value_get[obj_num][capacity];}int main(int argc, char** argv){int *weight;int *value;int *choice;int capacity;int obj_num;freopen("test.dat","r", stdin);while(scanf("%d%d",&capacity, &obj_num) != EOF){weight = (int*)malloc((obj_num + 1) * sizeof(int));value = (int*)malloc((obj_num + 1) * sizeof(int));choice = (int*)malloc((obj_num + 1) * sizeof(int));for(int i = 1; i <= obj_num; ++i){scanf("%d", weight + i);}for(int i = 1; i <= obj_num; ++i){scanf("%d", value + i);}int max_value = knapsack_dynamic(capacity,weight, value, choice, obj_num);printf("max_value = %d\n", max_value);for(int i = 1; i <= obj_num; ++i){printf("%d", choice[i]);}printf("\n");//free the spacefree(weight);free(value);free(choice);}return 0;}
0 0
- 0-1背包问题,动态规划求解
- 动态规划求解0-1背包问题
- 0/1背包问题-----动态规划求解
- 动态规划求解0-1背包问题
- 动态规划法求解简单的(0/1)背包问题
- 0/1背包问题的动态规划求解
- 0-1背包问题的动态规划求解
- 用动态规划求解0-1背包问题
- 0/1背包问题(动态规划求解)
- 用动态规划求解0-1背包问题
- Java 动态规划求解0-1背包问题
- 动态规划法——求解0-1背包问题
- 动态规划法——求解0-1背包问题
- 动态规划算法求解0,1背包问题
- 动态规划算法求解0-1背包问题
- 0——1背包问题动态规划求解
- 动态规划求解0 1背包
- 动态规划求解0-1背包
- Android中SQLite应用详解
- MFC中的消息 机制
- stm32的jatg口配置为普通io口使用
- 手把手,快速搭建 Cocos2d-HTML5 开发调试环境
- UVa-537
- 0-1背包问题的动态规划求解
- 风水学上八种住宅要避忌
- DVB--解复用
- [python]windows下模拟鼠标点击和键盘输入
- 六年级家长会教师讲话:向童年致敬
- JavaScript 右键禁用
- Cracking the coding interview--Q8.4
- this指针
- 惯性技术基础知识 --《惯性技术(国防工业大学出版社)》读书笔记(1)