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

多重背包

Posted by 1340502116 at 2016-03-09 11:23:47 on Problem 1276
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int W,n;
int w[10005];
int cnt;
int dp[100005];
int main()
{
	while(scanf("%d%d",&W,&n)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		cnt=0;
		for(int i=0;i<n;i++)
		{
			int b,v;
			scanf("%d%d",&b,&v);
			int e=1;
			while(e<=b)
			{
				w[cnt++]=e*v;
				b-=e;
				e<<=1;	
			}
			w[cnt++]=b*v;
		}
		
		for(int i=0;i<cnt;i++)
			for(int j=W;j>=w[i];j--)
				dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
		
		printf("%d\n",dp[W]);
	}
	
	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