01背包
来源:互联网 发布:三星note3破解4g网络 编辑:程序博客网 时间:2024/06/06 06:40
这是noip的基本功。
题目:有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。
解答:# include "iostream"
# include "cstdio"
using namespace std;
int
w[1000001],
c[1000001];
int
f[1001];
int main() {
int i,j,m,n;
int sc;
//freopen("package.in","r",stdin);
//freopen("package.out","w",stdout);
cin>>m;
cin>>n;
sc = 0;
for ( i=1; i<=n; i++ ) {
cin>>w[i];
sc += w[i];
cin>>c[i];
}//输入
for (i=1; i<=n; i++) {//代价
for ( j=w[i]; j<=m; j++ ) { //最大的价值
if(f[j-w[i]]+c[i]>f[j]){
f[j]=f[j-w[i]]+c[i];//看看这个是取还是不取,取最大值
}
}
}
printf("%d",f[m]);//输出
return 0;
}
阅读全文
1 0
- 【背包专题】01背包
- 01背包,完全背包
- 01背包 完全背包
- 01背包/完全背包
- 01背包,完全背包
- 背包问题---01背包
- 背包入门--01背包
- 【背包专题】01背包
- 01背包,完全背包
- 01背包,完全背包, 多重背包
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包详解
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包模板
- pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail
- 0817 T4 者
- mysql的合,交,并,差集
- Jmeter 入门教程(一)
- MyEclipse中Tomcat控制台打印乱码解决方案
- 01背包
- SDUT 2413-n a^o7 !
- Android中 完美实现 计时 倒计时 时间间隔处理等功能实现 by CountDownTimer
- Uber 和 Lyft 在德克斯萨大获全胜
- java 读取,修改properties文件,不改变文件内容顺序
- 简单java类(一对多关系)
- 浅解PCA
- Spring AOP 未导入四个包导致的错误:
- vue 开发中出现警告问题去除方法