[51nod]多重背包模板
https://www.51nod.com/tutorial/course.html#!courseId=11
题目大意:
有$N$种物品和一个容量为$W$的背包。第$i$种物品最多有$c[i]$件可用,每件体积是$w[i]$,价值是$v[i]$。求解将哪些物品装
入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
解题思路:采用二进制拆分的思想,将有限的背包划分为01背包和完全背包解决。
转移方程:$dp[i][j] = \max \{ dp[i - 1][v - k*w[i]] + k*v[i]|0 \le k \le c[i]\} $
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int c[],w[],v[],N,W;
ll dp[];
void bag01(int ww,int vv){
for(int i=W;i>=ww;i--){
dp[i]=max(dp[i],dp[i-ww]+vv);
}
}
void bagcomplete(int ww,int vv){
for(int i=ww;i<=W;i++){
dp[i]=max(dp[i],dp[i-ww]+vv);
}
}
void bagmult(){
memset(dp,,sizeof dp);
for(int i=;i<N;i++){
if(c[i]*w[i]>W){
bagcomplete(w[i],v[i]);
}
else{
int k=;
while(k<c[i]){
bag01(k*w[i],k*v[i]);
c[i]-=k;
k<<=;
}
bag01(c[i]*w[i],c[i]*v[i]);
}
}
} int main(){
scanf("%d%d",&N,&W);
for(int i=;i<N;i++){
scanf("%d%d%d",w+i,v+i,c+i);
}
bagmult();
printf("%lld",dp[W]);
}
[51nod]多重背包模板的更多相关文章
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- hdu2844Coins(多重背包模板)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活--hdu2191(多重背包模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 标准的多重背包 题目 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...
- 解题报告:hdu2191汶川地震 - 多重背包模板
2017-09-03 17:01:36 writer:pprp 这是一道多重背包裸题 - 记得是从右向左进行,还有几点需要注意啊,都在代码中表示出来了 代码如下: /* @theme:hdu2191 ...
- 多重背包模板 51Nod 1086
有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...
- 【多重背包模板】poj 1014
#include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...
- hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)
题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 ...
- 01背包模板、全然背包 and 多重背包(模板)
转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...
- 51nod 1086背包问题V2 (完全背包模板题)
1086 背包问题 V2 1 秒 131,072 KB 20 分 3 级题 题目描述 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1, ...
随机推荐
- 查看django的安装路径
查看django的安装路径 pip3 show django
- Nginx启动与停止
参考:https://www.phusionpassenger.com/library/install/nginx/install/oss/rubygems_rvm/ Starting Nginx Y ...
- 图片加载Picasso
https://github.com/square/picasso 基本用法 // 基本用法 // 普通加载图片 Picasso.with(PicassoActivity.this) .load(&q ...
- Data Structure Array: Find the two numbers with odd occurrences in an unsorted array
http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ #include ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode刷题笔记】Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- linux基本使用
(待完善,想到哪就写到哪,目前内容大幅度参考中文man手册) 最重要的命令(man) 在 Linux 下遇到问题,最重要的是要自己寻求帮助, google是个好东西 man 是 Linux 的帮助手册 ...
- 算法(Algorithms)第4版 练习 2.2.23
测试结果: 算法(Algorithms)第4版 练习 2.2.10 算法(Algorithms)第4版 练习 2.2.11(1) 算法(Algorithms)第4版 练习 2.2.11(2) 算法(A ...
- C#实现网站登录
public class HTMLHelper { /// <summary> /// 获取CooKie /// /// < ...
- MVC中URL传多个参数
1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...