当前位置:网站首页>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 */
边栏推荐
- Quick sort & merge sort
- Interface test tool postman
- Smart use of bitmap to achieve 100 million level massive data statistics
- 使用IDEAL连接数据库,运行出来了 结果显示一些警告,这部分怎么处理
- Pig-Latin (UVA492)
- Introduction to cloud native + container concept
- 接口测试--如何分析一个接口?
- idea灰屏问题
- DRF -- nested serializer (multi table joint query)
- 【作业】2022.5.24 MySQL 查操作
猜你喜欢

【模糊神经网络预测】基于模糊神经网络实现水质预测含Matlab源码

SQLyog导入数据库时报错,求帮解决!

【论文阅读|深读】Role2Vec:Role-Based Graph Embeddings

Day 10 data saving and loading

(Reprinted) an article will take you to understand the reproducing kernel Hilbert space (RKHS) and various spaces

Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column

NER中BiLSTM-CRF解读score_sentence

I get n offers in two months. I don't have any difficult interviewers here

About manipulator on Intelligent Vision Group

如何通过进程启动来分析和解决EasyCVR内核端口报错问题?
随机推荐
找到接口在表单里加参数
Cloud native -- websocket of Web real-time communication technology
Using virtual environments in jupyter notebook
[personal summary] learning plan
Error in conditional filter (if) syntax in sum function in SQL Server2005
RPC correction
Introduction to cloud native + container concept
Analysis of similarities and differences of various merged features (Union, merge, append, resolve) in ArcGIS
[operation] getting started with MySQL on May 23, 2022
Solutions for project paths
[punch in - Blue Bridge Cup] day 2 --- format output format, ASCII
利用反射整合ViewBinding和ViewHolder
Hebb and delta learning rules
(03). Net Maui actual combat basic control
dbt产品初体验
EasyCVR部署服务器集群时,出现一台在线一台不在线是什么原因?
Unity 在編輯器中輸入字符串時,轉義字符的輸入
The jupyter notebook kernel hangs up frequently and needs to be restarted
win10系统使用浏览器下载后,内容无故移动或删除
工程安全和工程质量