您的位置:首页 > 其它

51nod-1086 多重背包

2017-06-03 20:47 113 查看
#include <stdio.h>
#include <string.h>
#include <ctime>
#include <stack>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
#define lowbit(x) (x&-x)
#define mem(x,d) memset(x,d,sizeof(x))

const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;

int dp[maxn];

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0; i<n; i++)
{
int w,v,c,k;
scanf("%d%d%d",&w,&v,&c);
for(k=1; k<=c; k<<=1)
{
int u = k*w;
for(int j=m; j>=u; j--)
dp[j] = max(dp[j],dp[j-u]+k*v);
c -= k;
}
if(!c) continue;
int u = w*c;
for(int j=m; j>=u; j--)
dp[j] = max(dp[j],dp[j-u]+c*v);
}
cout<<dp[m]<<endl;
}
return 0;
}


有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输出可以容纳的最大价值。
Sample Input
3 6
2 2 5
3 3 8
1 4 1


Sample Output
9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: