当前位置:网站首页>背包问题 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 打印机脱机时一种容易被忽略的原因
- Appium自动化测试基础 — Appium测试环境搭建总结
- Shutter web hardware keyboard monitoring
- LeetCode 0107.二叉树的层序遍历II - 另一种方法
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- SQLMAP使用教程(二)实战技巧一
- One question per day 1020 Number of enclaves
- leetcode-6111:螺旋矩阵 IV
- Daily question 1189 Maximum number of "balloons"
猜你喜欢

SPI 详解

Navicat连接Oracle数据库报错ORA-28547或ORA-03135

QQ电脑版取消转义符输入表情

wordpress切换页面,域名变回了IP地址

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

Appium自动化测试基础 — Appium测试环境搭建总结

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135

4. Object mapping Mapster

1.14 - 流水线

MIT-6874-Deep Learning in the Life Sciences Week 7
随机推荐
【Rust 笔记】14-集合(下)
高斯消元 AcWing 884. 高斯消元解异或线性方程组
RGB LED infinite mirror controlled by Arduino
927. 三等分 模拟
The difference between CPU core and logical processor
Leetcode backtracking method
leetcode-9:回文数
Daily question 1189 Maximum number of "balloons"
Sum of three terms (construction)
MySQL advanced part 2: optimizing SQL steps
leetcode-556:下一个更大元素 III
MySQL advanced part 1: stored procedures and functions
Leetcode-6110: number of incremental paths in the grid graph
Leetcode dynamic programming
C Primer Plus Chapter 15 (bit operation)
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
数据可视化图表总结(一)
C - XOR to all (binary topic)
[rust notes] 14 set (Part 2)
Sword finger offer II 058: schedule