动态规划0-1背包问题
2016-02-19 12:04
579 查看
动态规划0-1背包问题
Ø
问题描述:
给定n种物品和一背包。物品i的重量是wi,其价值为vi,背包的容量为C。问应如何选择装入背包的物品,使得装
入背包中物品的总价值最大?
Ø
对于一种物品,要么装入背包,要么不装。所以对于一种物品的装入状态可以取0和1.我们设物品i的装入状态为xi,xi∈ (0,1),此问题称为0-11背包问题。
过程分析
数据:物品个数n=5,物品重量w
={0,2,2,6,5,4},物品价值V
={0,6,3,5,4,6},
(第0位,置为0,不参与计算,只是便于与后面的下标进行统一,无特别用处,也可不这么处理。)总重量c=10.
Ø背包的最大容量为10,那么在设置数组m大小时,可以设行列值为6和11,那么,对于m(i,j)就表示可选物品为i…n背包容量为j(总重量)时背包中所放物品的最大价值。
![](http://my.csdn.net/uploads/201204/15/1334501324_1452.png)
![](http://my.csdn.net/uploads/201204/15/1334501887_1644.png)
![](http://my.csdn.net/uploads/201204/15/1334501946_2751.png)
![](http://my.csdn.net/uploads/201204/15/1334501964_1118.png)
![](http://my.csdn.net/uploads/201204/15/1334502007_3475.png)
![](http://my.csdn.net/uploads/201204/15/1334502023_3289.png)
![](http://my.csdn.net/uploads/201204/15/1334502039_8212.png)
![](http://my.csdn.net/uploads/201204/15/1334502057_9558.png)
![](http://my.csdn.net/uploads/201204/15/1334502071_6464.png)
![](http://my.csdn.net/uploads/201204/15/1334502083_4160.png)
![](http://my.csdn.net/uploads/201204/15/1334502095_1855.png)
![](http://my.csdn.net/uploads/201204/15/1334502107_8866.png)
![](http://my.csdn.net/uploads/201204/15/1334502119_6750.png)
![](http://my.csdn.net/uploads/201204/15/1334502132_8242.png)
![](http://my.csdn.net/uploads/201204/15/1334502149_5180.png)
下面是自己写的源码:
[cpp] view
plain copy
print?
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<climits>
#include<cstring>
using namespace std;
const int c = 10; //背包的容量
const int w[] = {0,2,2,6,5,4};//物品的重量,其中0号位置不使用 。
const int v[] = {0,6,3,5,4,6};//物品对应的待加,0号位置置为空。
const int n = sizeof(w)/sizeof(w[0]) - 1 ; //n为物品的个数
int x[n+1];
void package0_1(int m[][11],const int w[],const int v[],const int n)//n代表物品的个数
{
//采用从底到顶的顺序来设置m[i][j]的值
//首先放w
for(int j = 0; j <= c; j++)
if(j < w
) m
[j] = 0; //j小于w
,所对应的值设为0,否则就为可以放置
else m
[j] = v
;
//对剩下的n-1个物品进行放置。
int i;
for(i = n-1; i >= 1; i--)
for(int j = 0; j <= c; j++)
if(j < w[i])
m[i][j] = m[i+1][j];//如果j < w[i]则,当前位置就不能放置,它等于上一个位置的值。
//否则,就比较到底是放置之后的值大,还是不放置的值大,选择其中较大者。
else m[i][j] = m[i+1][j] > m[i+1][j-w[i]] + v[i]?
m[i+1][j] : m[i+1][j-w[i]] + v[i];
}
void answer(int m[][11],const int n)
{
int j = c;
int i;
for(i = 1; i <= n-1; i++)
if(m[i][j] == m[i+1][j]) x[i] = 0;
else {
x[i] = 1;
j = j - w[i];
}
x
= m[i][j] ? 1 : 0;
}
int main()
{
int m[6][11]={0};
package0_1(m,w,v,n);
for(int i = 0; i <= 5; i++)
{
for(int j = 0; j <= 10; j++)
printf("%2d ",m[i][j]);
cout << endl;
}
answer(m,n);
cout << "The best answer is:\n";
for(int i = 1; i <= 5; i++)
cout << x[i] << " ";
system("pause");
return 0;
}
Ø
问题描述:
给定n种物品和一背包。物品i的重量是wi,其价值为vi,背包的容量为C。问应如何选择装入背包的物品,使得装
入背包中物品的总价值最大?
Ø
对于一种物品,要么装入背包,要么不装。所以对于一种物品的装入状态可以取0和1.我们设物品i的装入状态为xi,xi∈ (0,1),此问题称为0-11背包问题。
过程分析
数据:物品个数n=5,物品重量w
={0,2,2,6,5,4},物品价值V
={0,6,3,5,4,6},
(第0位,置为0,不参与计算,只是便于与后面的下标进行统一,无特别用处,也可不这么处理。)总重量c=10.
Ø背包的最大容量为10,那么在设置数组m大小时,可以设行列值为6和11,那么,对于m(i,j)就表示可选物品为i…n背包容量为j(总重量)时背包中所放物品的最大价值。
![](http://my.csdn.net/uploads/201204/15/1334501324_1452.png)
![](http://my.csdn.net/uploads/201204/15/1334501887_1644.png)
![](http://my.csdn.net/uploads/201204/15/1334501946_2751.png)
![](http://my.csdn.net/uploads/201204/15/1334501964_1118.png)
![](http://my.csdn.net/uploads/201204/15/1334502007_3475.png)
![](http://my.csdn.net/uploads/201204/15/1334502023_3289.png)
![](http://my.csdn.net/uploads/201204/15/1334502039_8212.png)
![](http://my.csdn.net/uploads/201204/15/1334502057_9558.png)
![](http://my.csdn.net/uploads/201204/15/1334502071_6464.png)
![](http://my.csdn.net/uploads/201204/15/1334502083_4160.png)
![](http://my.csdn.net/uploads/201204/15/1334502095_1855.png)
![](http://my.csdn.net/uploads/201204/15/1334502107_8866.png)
![](http://my.csdn.net/uploads/201204/15/1334502119_6750.png)
![](http://my.csdn.net/uploads/201204/15/1334502132_8242.png)
![](http://my.csdn.net/uploads/201204/15/1334502149_5180.png)
下面是自己写的源码:
[cpp] view
plain copy
print?
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<climits>
#include<cstring>
using namespace std;
const int c = 10; //背包的容量
const int w[] = {0,2,2,6,5,4};//物品的重量,其中0号位置不使用 。
const int v[] = {0,6,3,5,4,6};//物品对应的待加,0号位置置为空。
const int n = sizeof(w)/sizeof(w[0]) - 1 ; //n为物品的个数
int x[n+1];
void package0_1(int m[][11],const int w[],const int v[],const int n)//n代表物品的个数
{
//采用从底到顶的顺序来设置m[i][j]的值
//首先放w
for(int j = 0; j <= c; j++)
if(j < w
) m
[j] = 0; //j小于w
,所对应的值设为0,否则就为可以放置
else m
[j] = v
;
//对剩下的n-1个物品进行放置。
int i;
for(i = n-1; i >= 1; i--)
for(int j = 0; j <= c; j++)
if(j < w[i])
m[i][j] = m[i+1][j];//如果j < w[i]则,当前位置就不能放置,它等于上一个位置的值。
//否则,就比较到底是放置之后的值大,还是不放置的值大,选择其中较大者。
else m[i][j] = m[i+1][j] > m[i+1][j-w[i]] + v[i]?
m[i+1][j] : m[i+1][j-w[i]] + v[i];
}
void answer(int m[][11],const int n)
{
int j = c;
int i;
for(i = 1; i <= n-1; i++)
if(m[i][j] == m[i+1][j]) x[i] = 0;
else {
x[i] = 1;
j = j - w[i];
}
x
= m[i][j] ? 1 : 0;
}
int main()
{
int m[6][11]={0};
package0_1(m,w,v,n);
for(int i = 0; i <= 5; i++)
{
for(int j = 0; j <= 10; j++)
printf("%2d ",m[i][j]);
cout << endl;
}
answer(m,n);
cout << "The best answer is:\n";
for(int i = 1; i <= 5; i++)
cout << x[i] << " ";
system("pause");
return 0;
}
相关文章推荐
- !!!??? 2.3 核心模块与应用程序的对比
- symfony对于mysql自动设置时间字段
- 动态规划之01背包问题(最易理解的讲解)
- 三星Note2(N7100)刷机
- 谷歌设计师的Material Design实践心得
- PL/SQL教程之五:异常错误处理
- Item 49:new handler的行为
- 【转】深入理解篇UIScrollerView
- C#资源释放方法实例分析
- mysql启动报错:Another MySQL daemon already running with the same unix socket.
- 即时搜索input propertychange的bug
- mysql 中文乱码问题
- JDBC连接mysql<PreparedStatement>
- C# 中DateTime的各种使用
- Item 48:了解模板元编程
- 用shape结合selector实现点击效果
- 移动web:转盘抽奖(幸运大转盘)
- Redis数据库的使用场景介绍(避免误用Redis)
- Windows Phone 10(Lumia 920)升级记录
- Mac 配置php开发环境