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

多重背包,怎么老是RE,无语了,看看

Posted by dynamic_study at 2009-08-06 20:44:00 on Problem 1742
#include<iostream>
using namespace std;
#define maxn 100001
#define maxn1 220
int m;
void complete_bag(int n,int V,int value[],int weight[],int num[])
{
	int dp[maxn1][maxn];
	int i,j,k,ans=0;;
	for(i=0;i<=n;i++)
		dp[i][0]=0;
	for(j=0;j<=V;j++)
		dp[0][j]=0;
	for(i=1;i<=n;i++)  
		for(j=1;j<=V;j++)
		{
			for(k=0;k<=num[i]&&k*weight[i]<=j;k++)
			{
			   if(j<k*weight[i])
				  dp[i][j]=dp[i-1][j];
			   else
			   {
				if(dp[i-1][j]>dp[i-1][j-k*weight[i]]+k*value[i])
					dp[i][j]=dp[i-1][j];
				else
					dp[i][j]=dp[i-1][j-k*weight[i]]+k*value[i];
			   }
			}
		}
	for(j=1;j<=m&&j<=V;j++)
	 if(dp[n][j]==j)
		  ans++;
	 cout<<ans<<endl;


}

int main()
{
	int n,v,i;
	int value[maxn1],num[maxn1];
	while(cin>>n>>m&&n||m)
	{
	for(i=1;i<=n;i++)
		cin>>value[i];
	v=0;
	for(i=1;i<=n;i++)
	{
		cin>>num[i];
		v+=num[i]*value[i];
	}
	complete_bag(n,v,value,value,num);
	}
	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