当前位置:网站首页>01 backpack, dynamic planning
01 backpack, dynamic planning
2022-06-30 04:06:00 【Cod_ ing】
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=10;
const int MAXW=100;
int w[MAXW]; // Weight of goods
int v[MAXN]; // Value of goods
int dp[MAXN][MAXW];
int n,c; // The quantity and value of the goods
void traceback();
void detail();
int main()
{
cin>>n>>c;
cout<<" Enter the weight and value of the goods "<<endl;
for(int i=1;i<=n;i++) cin>>w[i]>>v[i];
for(int i=1;i<=n;i++)
{
for(int j=0;j<=c;j++)
{
if(w[i]>j)
dp[i][j]=dp[i-1][j];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
}
}
cout<<dp[n][c]<<endl;
traceback();
detail();
}
void detail()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<=c;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}
}
void traceback()
{
int x[MAXN];
int temp=c;
for(int i=n;i>=1;i--)
{
if(dp[i][temp]==dp[i-1][temp])
x[i]=0;
else
{
x[i]=1;
temp-=w[i];
}
}
for(int i=1;i<=n;i++)
if(x[i])
cout<<i<<" ";
cout<<endl;
}
/* The test case : 1 10 2 12 3 13 3 15 4 15 5 16 */
边栏推荐
- GIS related data
- [note] May 23, 2022 MySQL
- Solutions for project paths
- Thinkphp5 implements import function
- [note] on May 28, 2022, data is obtained from the web page and written into the database
- ThingsBoard教程(二三):在规则链中计算二个设备的温度差
- 关于智能视觉组上的机械臂
- 接口测试--如何分析一个接口?
- Use ideal to connect to the database. The results show some warnings. How to deal with this part
- 知识点滴 - 如何用3个简单的技巧在销售中建立融洽的关系
猜你喜欢

ThingsBoard教程(二三):在规则链中计算二个设备的温度差
![[punch in - Blue Bridge Cup] day 2 --- format output format, ASCII](/img/b2/0059659867e867a32b8e7cef567c8b.jpg)
[punch in - Blue Bridge Cup] day 2 --- format output format, ASCII

RPC correction

第十一天 脚本与游戏AI

DBT product initial experience

基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题

(04). Net Maui actual MVVM

在大厂外包呆了三年,颠覆了我的认知!

Node red series (28): communication with Siemens PLC based on OPC UA node

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
随机推荐
【笔记】2022.5.23 MySQL
云原生入门+容器概念介绍
Green new power and "zero" burden of computing power -- JASMINER X4 series is popular
[summary of skimming questions] database questions are summarized by knowledge points (continuous update / simple and medium questions have been completed)
Semantic segmentation resources
技术分享| 融合调度中的广播功能设计
Errno and PERROR
I spent three years in a big factory outsourcing, which subverted my understanding!
SQLyog导入数据库时报错,求帮解决!
Day 11 script and game AI
如何利用FME 创建自己的功能软件
How to solve the problem of link hyperlinks when trying to link the database?
Selenium environment installation, 8 elements positioning --01
Interface test tool postman
Sql语句遇到的错误,求解
How to analyze and solve the problem of easycvr kernel port error through process startup?
ThingsBoard教程(二三):在规则链中计算二个设备的温度差
Radiant energy, irradiance and radiance
Node red series (28): communication with Siemens PLC based on OPC UA node
Interpretation score of bilstm-crf in NER_ sentence