Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

AC!我没有使用滚动数组,望高手不吝踢教!

Posted by lexdene at 2009-04-05 09:26:37 on Problem 3624
我没有使用滚动数组,只是普通的01背包问题的解法。
C++新手,望高手不吝赐教!
#include<iostream>
using namespace std;
int main(){
	int n,m,i,j,*w,*v,*f;
	while(cin>>n>>m){
	//申请空间
	w=new int[n];
	v=new int[n];
	f=new int[m+1];
	//读入数据
	for(i=0;i<n;i++)
		cin>>w[i]>>v[i];
	//数据初始化
	for(i=0;i<=m;i++) f[i]=0;
	//计算过程
	for(i=0;i<n;i++)
		for(j=m;j>=w[i];j--)
			if(f[j-w[i]]+v[i]>f[j])
				f[j]=f[j-w[i]]+v[i];
	//求最大值
	int max=-1;
	for(i=0;i<=m;i++)
		if(f[i]>max) max=f[i];
	//输出结果
	cout<<max<<endl;
	//释放空间
	delete w,v,f;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator