当前位置:网站首页>背包问题 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 【LeetCode】Easy | 20. Valid parentheses
- Golang uses context gracefully
- MySQL advanced part 2: MySQL architecture
- leetcode-556:下一个更大元素 III
- One question per day 1447 Simplest fraction
- leetcode-9:回文数
- 【LeetCode】Day94-重塑矩阵
- Redis publish subscribe command line implementation
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- leetcode-1200:最小绝对差
猜你喜欢
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Redis publish subscribe command line implementation
Appium基础 — 使用Appium的第一个Demo
LeetCode 0107.二叉树的层序遍历II - 另一种方法
LeetCode-61
Doing SQL performance optimization is really eye-catching
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
数据可视化图表总结(二)
Leetcode-6110: number of incremental paths in the grid graph
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
随机推荐
MySQL advanced part 1: stored procedures and functions
Leetcode-6111: spiral matrix IV
MySQL advanced part 2: SQL optimization
【Rust 笔记】13-迭代器(中)
wordpress切换页面,域名变回了IP地址
LeetCode-61
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
leetcode-3:无重复字符的最长子串
QQ电脑版取消转义符输入表情
TypeScript 基础讲解
leetcode-6109:知道秘密的人数
[rust notes] 16 input and output (Part 2)
[rust notes] 13 iterator (Part 2)
[rust notes] 17 concurrent (Part 2)
Leetcode array operation
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
【Rust 笔记】17-并发(上)
QQ computer version cancels escape character input expression
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
leetcode-556:下一个更大元素 III