由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - choose three items of various weights to will fit into a knapsack of capacity 5
相关主题
问个C++读入文件的问题c++,如何用cout 输出double 变量
请问C语言fscanf的用法请教一个Gaussian quadrature的问题
继续我们计算non-prime number 的探险[转载] matlab算weighted least square
c++, sort() 为啥显示 结果不对how to do weights optimization?
请问一个基本的minimization problem有没有近似解法?请教一个算法
C++ questionsPaper help!
请问为什么我的程序不能background运行?请问怎样让一m长的向量和一mxn的矩阵相乘,仍为mxn
求助:C++里的fstream究竟该怎么用? (转载)Bioinformatics Software Developer (Bethesda, MD)
相关话题的讨论汇总
话题: int话题: capacity话题: num话题: value话题: weight
进入Computation版参与讨论
1 (共1页)
f******5
发帖数: 11
1
//Algorithm DPKnapsack(w[1..n], v[1..n], W)
// var V[0..n,0..W], P[1..n,1..W]: int
// for j := 0 to W do
// V[0,j] := 0
// for i := 0 to n do
// V[i,0] := 0
// for i := 1 to n do
// for j := 1 to W do
// if w[i]  j and v[i] + V[i-1,j-w[i]] > V[i-1,j] then
// V[i,j] := v[i] + V[i-1,j-w[i]]; P[i,j] := j-w[i]
// else
// V[i,j] := V[i-1,j]; P[i,j] := j
// return V[n,W] and the optimal subset by backtracing
//choose three items that will fit into a knapsack of capacity 5
#include
using namespace std;
void knapsack(int *weight, int *value, int num, int capacity);
void knapsack(int *weight, int *value, int num, int capacity){
int V[num+1][capacity+1];
int K[num+1][capacity+1];
for (int j=0; j V[0][j]=0;
} //initialize the value matrix first row
for (int i=0; i V[i][0]=0;
}//initialize the value matrix[][] first column
for(int i=0; i for(int j=0; j K[i][j]=0;
V[i][j]=0;
}

for (int i=1; i for (int w=1; w if(w>=weight[i]&&value[i]+V[i-1][w-weight[i] ]> V[i-1][w])
{
K[i][w]=1;//K is the keep matrix determing whether to keep
item i
V[i][w]= value[i]+V[i-1][(w-weight[i])];//V is the value
matrix indicating the current value in the sack
} //value[i] is the value of worth in item i branch put
item i into the sack and leave availabe w-weight[i] for i-1 th item
else
{
K[i][w]=0;//not the keep item i in the sack so 0
V[i][w]=V[i-1][w]; //value of present comprenesive item
number updated by 1 lesss than total item
}

}//end of inner for loop inside double for loop reassign the value
matrix and keep matrix
}//end of outer for loop i represents the item 1, 2,3
for (int i=num, j=capacity; i>0;i--) {
if (K[i][j]==1)//start from the bottom right corner of Keep matrix
of capacity weight and total item
{
cout<<"put into the backsack item number#: "< j=j-weight[i];
}
}

for (int i=1;i for (int j=1; j {
cout<
}
cout< }
for (int i=1;i for (int j=1; j {
cout<
}
cout< }
}
int main(void)
{
int weight[]={0,3,2,1};
int value[]={0,5,3,4};
int capacity=5;
int num=3;
knapsack(weight, value, num, capacity);

system("pause");
return 0;
}
1 (共1页)
进入Computation版参与讨论
相关主题
Bioinformatics Software Developer (Bethesda, MD)请问一个基本的minimization problem有没有近似解法?
Invited Review Articles at JPRC++ questions
Is it possible to initialize container in initialization list?请问为什么我的程序不能background运行?
A question about C++. Thanks.求助:C++里的fstream究竟该怎么用? (转载)
问个C++读入文件的问题c++,如何用cout 输出double 变量
请问C语言fscanf的用法请教一个Gaussian quadrature的问题
继续我们计算non-prime number 的探险[转载] matlab算weighted least square
c++, sort() 为啥显示 结果不对how to do weights optimization?
相关话题的讨论汇总
话题: int话题: capacity话题: num话题: value话题: weight