HDU_3466_动规(01背包)
2015-12-20 16:19
477 查看
#include<bits/stdc++.h> using namespace std; const int maxn = 500 + 5; const int maxm = 5000 + 5; typedef struct node{ int p, q, v; bool operator< (const node x) const { return (q-p)<(x.q-x.p); } }Node; Node c[maxn]; int F[maxn][maxm]; int main() { int n, m; while(cin >> n >> m) { for(int i = 1; i <= n; i++) { cin >> c[i].p >> c[i].q >> c[i].v; } sort(c+1, c+n+1); memset(F, 0, sizeof(F)); for(int i = 1; i<= n; i++) { for(int j = 0; j <= m; j++) { F[i][j] = (i ==1 ? 0: F[i-1][j]); if(j >= c[i].q) F[i][j] = max(F[i][j], F[i-1][j-c[i].p]+c[i].v); } } cout << F [m] << endl; } return 0; }
使用滚动数组:
#include<bits/stdc++.h> using namespace std; const int maxn = 500 + 5; const int maxm = 5000 + 5; typedef struct node{ int p, q, v; bool operator< (const node x) const { return (q-p)<(x.q-x.p); } }Node; Node c[maxn]; int F[maxm]; int main() { int n, m; while(cin >> n >> m) { for(int i = 1; i <= n; i++) { cin >> c[i].p >> c[i].q >> c[i].v; } sort(c+1, c+n+1); memset(F, 0, sizeof(F)); for(int i = 1; i<= n; i++) { for(int j = m; j >= 0; j--) { F[j] = (i ==1 ? 0: F[j]); if(j >= c[i].q) F[j] = max(F[j], F[j-c[i].p]+c[i].v); } } cout << F[m] << endl; } return 0; }
相关文章推荐
- html5 引入什么新的表单属性
- 数据结构小结 说明
- TableLayout实现登录界面
- 高性能,高并发
- 用MXnet实战深度学习之一:安装GPU版mxnet并跑一个MNIST手写数字识别
- 【HDU】5248-序列变换(贪心+二分)
- html5表单数据如何获得
- mysql 排序后获得某行的位置
- 返回顶部
- python-markdown无法将```生成标签问题解决方法
- Windows平台下写文件的回车换行问题
- Java JUnit4
- wordpress常见问题
- Java开发环境的基本设置
- Google C++ 风格指南 命名约定 转
- 求一个HTML5 表单的写法例子
- URL重写
- firefox 插件 Flash 已崩溃 重新加载页面
- 数据结构小结(九)排序算法大杂烩
- ubuntu-sqlite install