۱۳-اسفند-۱۳۸۸, ۲۳:۴۳:۵۹
کد php:
#include<iostream>
using namespac e std;
int zero_knaspsack(int n,int *w,int *p,int c[][11],int M,int T[][11])
{
for (int j=0;j<=M;j++)
c[0][j]=0;
for(int i;i<=n;i++)
for(j=0;j<=M;j++){
if(j<w[i]){
c[i][j]=c[i-1][j];
T[i][j]=0;
}
else{
if(c[i-1][j]>c[i-1][j-w[i]]+p[i]){
c[i][j]=c[i-1][j];
T[i][j]=1;
}
}
}
j=M;
for(i=n;i>=1;i--){
if(T[i][j]==1){
cout<<endl<<"X["<<i<<"]="<<1;
j=j-w[i];
}
else
cout<<endl<<"X["<<i<<"]="<<0;
}
return c[n][M];
}
int w[4]={0,4,5,7};
int p[4]={0,24,70,49};
int M=10;
int cost[4][11];
int track[4][11];