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:多重背包转化为01背包,我的63ms才过,换成完全背包再写下。。。

Posted by lijingwei at 2011-08-17 15:16:48 on Problem 1276
In Reply To:Re:多重背包转化为01背包,我的63ms才过,换成完全背包再写下。。。 Posted by:jayhaizeizai at 2011-08-17 15:13:50
01背包转化是这样的,格式懒得对齐了。。。
for(i=1;i<=num;i++)
{
	scanf("%d%d",&cnt[i],&t);
            j=1;
            while(j<=cnt[i])
            {
                cnt[i]-=j;
                value[index++]=j*t;
                j*=2;
            }
            if(cnt[i])//处理多余的
	            value[index++]=cnt[i]*t;
}
		memset(dp,false,(n+10)*sizeof(bool));
		dp[0]=true;
		for(i=0;i<index;i++)//01背包
		{
			for(j=n;j>=value[i];j--)//01背包的循环
			{
				if(dp[j-value[i]])
				{
					dp[j]=true;
					if(j>max)
						max=j;
				}
			}
		}
		printf("%d\n",max);

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