51nod1086 背包问题 V2

我都快不会写二进制优化多重背包了。。。卡了一下常数从rank100+到20+。。。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
}
const int nmax=105;
const int maxn=5e4+5;
const int inf=0x7f7f7f7f;
int tw[nmax],tv[nmax],c[nmax];
int w[maxn],v[maxn];
int dp[maxn];
void maxs(int &a,int b){
	if(a<b) a=b;
}
int main(){
	int n=read(),m=read(),cnt=0;
	rep(i,1,n) tw[i]=read(),tv[i]=read(),c[i]=read();
	rep(i,1,n){
		int j,ta=tw[i],tb=tv[i],&tc=c[i];
		for(j=0;(1<<j)<tc;j++) w[++cnt]=ta*(1<<j),v[cnt]=tb*(1<<j),tc-=(1<<j);
		w[++cnt]=ta*tc;v[cnt]=tb*tc;
	}
	rep(i,1,cnt) dwn(j,m,w[i]) maxs(dp[j],dp[j-w[i]]+v[i]);
	printf("%d
",dp[m]);
	return 0;
}

  

基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注
有N种物品,每种物品的数量为C1,C2......Cn。从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数)。求背包能够容纳的最大价值。
 
Input
第1行,2个整数,N和W中间用空格隔开。N为物品的种类,W为背包的容量。(1 <= N <= 100,1 <= W <= 50000)
第2 - N + 1行,每行3个整数,Wi,Pi和Ci分别是物品体积、价值和数量。(1 <= Wi, Pi <= 10000, 1 <= Ci <= 200)
Output
输出可以容纳的最大价值。
Input示例
3 6
2 2 5
3 3 8
1 4 1
Output示例
9
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5862918.html