Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:多重背包转化为01背包,我的63ms才过,换成完全背包再写下。。。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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator