当前位置:网站首页>背包问题 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 927. 三等分 模拟
- LeetCode-54
- Redis publish subscribe command line implementation
- Shutter web hardware keyboard monitoring
- Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
- Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
- One question per day 1447 Simplest fraction
- Leetcode divide and conquer / dichotomy
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- Leetcode-6110: number of incremental paths in the grid graph
猜你喜欢

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

传统数据库逐渐“难适应”,云原生数据库脱颖而出

Data visualization chart summary (II)

Leetcode-6108: decrypt messages

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

SQLMAP使用教程(一)

MySQL advanced part 1: index

可变电阻器概述——结构、工作和不同应用

Is it impossible for lamda to wake up?
随机推荐
数据可视化图表总结(二)
LeetCode 1200. Minimum absolute difference
Redis publish subscribe command line implementation
1.15 - input and output system
1040 Longest Symmetric String
Leetcode-556: the next larger element III
Leetcode dynamic programming
One question per day 1447 Simplest fraction
leetcode-556:下一个更大元素 III
Leetcode-3: Longest substring without repeated characters
Leetcode-1200: minimum absolute difference
Appium自动化测试基础 — Appium测试环境搭建总结
Appium基础 — 使用Appium的第一个Demo
Open source storage is so popular, why do we insist on self-development?
Leetcode-9: palindromes
MIT-6874-Deep Learning in the Life Sciences Week 7
TypeScript 基础讲解
Quickly use Amazon memorydb and build your own redis memory database
1039 Course List for Student
Sqlmap tutorial (1)