0_1背包问题

//此问题由于在暑假看过提示,这里算搬运的了

暑假看的讲解在这里  http://blog.csdn.net/dapengbusi/article/details/7463968



//这是空间浪费型的
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;


ifstream fin("C:\\data16.in");


int C;
int n;
int weight[100];
int bulk[100];
int scheme[1000][1000];

//加sort让调试的过程看起来更舒服,对结果没有影响,可以去掉
void sort()
{
     for(int i=1;i<n;++i)
     {
         int temp;
         for(int j=i;j>0;--j)
         {
             if(bulk[j]<bulk[j-1])
             {
                 temp=bulk[j];
                 bulk[j]=bulk[j-1];
                 bulk[j-1]=temp;
                 temp=weight[j];
                 weight[j]=weight[j-1];
                 weight[j-1]=temp;
             }
             else if(bulk[j]==bulk[j-1]&&weight[j]<weight[j-1])
             {
                 temp=weight[j];
                 weight[j]=weight[j-1];
                 weight[j-1]=temp;
             }
             else
                 break;
         }
     }
}


void Init()
{
fin>>C;
int w,c;
n=0;
while(fin>>c>>w)
{
bulk[n]=c;
weight[n++]=w;
}
sort();
cout<<"C="<<C<<endl;
for(int i=0;i<n;++i)
cout<<"(bulk:"<<bulk[i]<<",weight:"<<weight[i]<<")\t";
cout<<endl;
system("pause");
}


void dp()
{
for(int i=0;i<=C;++i)
{
if(i<bulk[0])
scheme[0][i]=0;
else
scheme[0][i]=weight[0];
}
for(int i=1;i<n;++i)
{
for(int j=0;j<bulk[i];++j)
scheme[i][j]=scheme[i-1][j];
for(int j=bulk[i];j<=C;++j)
scheme[i][j]=(weight[i]+scheme[i-1][j-bulk[i]])>scheme[i-1][j]?(weight[i]+scheme[i-1][j-bulk[i]]):scheme[i-1][j];
}
for(int i=0;i<n;++i)
{
        for(int j=0;j<=C;++j)
        {
            cout<<scheme[i][j]<<"\t";
        }
        cout<<endl;
    }
    system("pause");
}


void print(int c,int startpos)
{
if(c>0)
{
int i;
for(i=startpos;i>=0;--i)
if(scheme[i][c]!=scheme[i-1][c])
break;
cout<<"("<<bulk[i]<<","<<weight[i]<<")\t";
print(c-bulk[i],i-1);
}
}


int main()
{
Init();
dp();
print(C,n-1);
system("pause");
return 0;
}



#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;


ifstream fin("C:\\data16.in");

//这是空间节约型的

int C;
int n;
int weight[100];
int bulk[100];
int scheme[1000];
int parent[1000];


void Init()
{
fin>>C;
int w,c;
n=0;
while(fin>>c>>w)
{
bulk[n]=c;
weight[n++]=w;
}
memset(scheme,0,sizeof(scheme));
memset(parent,-1,sizeof(parent));
}


void DP()
{
for(int i=0;i<n;++i)
{
for(int j=bulk[i];j<=C;++j)
{
if(scheme[j-bulk[i]]+weight[i]>scheme[j]&&parent[j-bulk[i]]!=i)
{
scheme[j]=scheme[j-bulk[i]]+weight[i];
parent[j]=i;
}
}
    }
}


void print()
{
for(int i=C;i>0;)
{
while(scheme[i]==scheme[i-1])
--i;
cout<<"("<<bulk[parent[i]]<<","<<weight[parent[i]]<<")\t";
i-=bulk[parent[i]];
}
}


int main()
{
Init();
DP();
print();
system("pause");
return 0;
}



//以下代码是对第一段空间浪费型代码的化简,写的太漂亮了

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

ifstream fin("C:\\data16.in");

int C;
int n;
int weight[100];
int bulk[100];
int scheme[1000][1000];

void Init()
{
	fin>>C;
	int w,c;
	n=0;
	while(fin>>c>>w)
	{
		bulk[n]=c;
		weight[n++]=w;
	}
	cout<<"C="<<C<<endl;
	for(int i=0;i<n;++i)
	cout<<"(bulk:"<<bulk[i]<<",weight:"<<weight[i]<<")\t";
	cout<<endl;
	system("pause");
}

void dp()
{
	for(int i=n;i>=0;--i)
	{
        for(int j=0;j<=C;++j)
        {
            scheme[i][j]=(i==n?0:scheme[i+1][j]);
            if(j>=bulk[i])
                scheme[i][j]>?=scheme[i+1][j-bulk[i]]+weight[i];
        }
    }
}

void print(int c,int startpos)
{
	if(c>0)
	{
        while(scheme[startpos][c]==scheme[startpos+1][c])
            ++startpos;
        cout<<"(bulk:"<<bulk[startpos]<<",weight:"<<weight[startpos]<<")\t";
        print(c-bulk[startpos],startpos+1);
	}
}

int main()
{
	Init();
	dp();
	print(C,0);
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值