当前位置:网站首页>背包问题 AcWing 9. 分组背包问题
背包问题 AcWing 9. 分组背包问题
2022-07-05 06:16:00 【T_Y_F666】
背包问题 AcWing 9. 分组背包问题
原题链接
算法标签
背包问题 DP
思路
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 105;
int s[N];
int v[N][N], w[N][N],f[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), vv=read();
rep(i, 1, n+1){
s[i]=read();
rep(j, 0, s[i]){
v[i][j]=read(), w[i][j]=read();
}
}
// n组物品
rep(i, 1, n+1){
// 背包剩余容量 由于物品只能使用一次 因此从大到小循环
Rep(j, vv, 0){
// 第i组中选取一个物品使价值最大
rep(k, 0, s[i]){
if(v[i][k]<=j){
f[j]=max(f[j], f[j-v[i][k]]+w[i][k]);
}
}
}
}
printf("%lld", f[vv]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 11-gorm-v2-02-create data
- LeetCode 1200. Minimum absolute difference
- LVS简介【暂未完成(半成品)】
- Leetcode heap correlation
- 【LeetCode】Day95-有效的数独&矩阵置零
- Binary search template
- Shutter web hardware keyboard monitoring
- MySQL advanced part 2: storage engine
- Overview of variable resistors - structure, operation and different applications
- Leetcode-9: palindromes
猜你喜欢
LeetCode-54
Chapter 6 relational database theory
MIT-6874-Deep Learning in the Life Sciences Week 7
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
SPI 详解
Overview of variable resistors - structure, operation and different applications
SQLMAP使用教程(二)实战技巧一
Simple selection sort of selection sort
Quickly use Amazon memorydb and build your own redis memory database
liunx启动redis
随机推荐
1040 Longest Symmetric String
QQ电脑版取消转义符输入表情
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
Groupbykey() and reducebykey() and combinebykey() in spark
Sqlmap tutorial (II) practical skills I
redis发布订阅命令行实现
leetcode-6111:螺旋矩阵 IV
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
【LeetCode】Day95-有效的数独&矩阵置零
4. Object mapping Mapster
Leetcode dynamic programming
1.13 - RISC/CISC
[rust notes] 14 set (Part 1)
WordPress switches the page, and the domain name changes back to the IP address
Leetcode-6110: number of incremental paths in the grid graph
Overview of variable resistors - structure, operation and different applications
927. Trisection simulation
MySQL advanced part 1: stored procedures and functions
Sqlmap tutorial (1)
【LeetCode】Day94-重塑矩阵