SDUN上部分背包问题及解析
2017-11-10 13:01
239 查看
1.首先是1033这道题
纯纯正正赤赤裸裸的01背包问题
做这种问题时,设ly是利益,设zl是限制条件,有n个东西,限制的最大条件w;
下面是思路
贴代码
2.完全背包问题
例如1360这道题
其实差不多思路,就是改改循环顺序;
做这种问题时,设ly是利益,设zl是限制条件,有n个东西,限制的最大条件w;
下面是思路
1360
贴代码
纯纯正正赤赤裸裸的01背包问题
做这种问题时,设ly是利益,设zl是限制条件,有n个东西,限制的最大条件w;
下面是思路
int b,c; int a=[w+1]; //a[x] 表示背包容量为x 时的最大价值 for ( b=0; b<n; b++) for ( c=w; c>=zl[i]; c--) if(a[c]<a[c-zl[i]]+li[i]) a[c]=a[c-zl[i]]+li[i];
贴代码
#include<iostream> #include<cstdio> using namespace std; int main() { int a, b, c, d, e, f; scanf("%d %d", &a, &b); int t[101], j[101], s[1001] = {0}; for (c = 1; c <= b; c++) { scanf("%d %d", &t[c], &j[c]); } for (c = b; c >0; c--) { for (d = a; d >=t[c]; d--) { if (s[d] < s[d - t[c]] + j[c]) { s[d] = s[d - t[c]] + j[c]; } } } printf("%d\n", s[a]); return 0; }
2.完全背包问题
例如1360这道题
其实差不多思路,就是改改循环顺序;
做这种问题时,设ly是利益,设zl是限制条件,有n个东西,限制的最大条件w;
下面是思路
int b,c; int a=[w+1]; //a[x] 表示背包容量为x 时的最大价值 for ( b=0; b<n; b++) for ( c=zl[i]; c<=w; c++) if(a[c]<a[c-zl[i]]+li[i]) a[c]=a[c-zl[i]]+li[i];
1360
贴代码
#include<iostream> #include<cstdio> using namespace std; int main() { int a, b, c, d, e, f; int r[101], x[101]; while (cin >> a) { int m[100000] = { 0 }; for (b = 1; b <= a; b++) { cin >> x[b] >> r[b]; } cin >> d; for (c = 1; c <= a; c++) { for (e = r[c]; e <= d; e++) { if (m[e] < m[e - r[c]] + x[c]) { m[e] = m[e - r[c]] + x[c]; } } } printf("%d\n", m[d]); } return 0; }
相关文章推荐
- 8.4.2(部分背包问题)
- 经典算法6:贪心算法解决部分背包问题
- 关于poi解析word和excel的问题,解析word中的table,获取部分table的行数和列数不正确
- 贪心算法解决部分背包问题 在O(lgn)时间内
- 贪心法:部分背包问题
- 多重部分和问题 多重背包&& Coins POJ - 1742
- 多重部分和问题(多重背包+二进制优化)
- ACM知识点 之 贪心(4)部分背包问题
- 【算法】贪心策略实现部分背包问题
- 部分背包问题模板
- 多重部分和问题 多重背包&& Coins POJ - 1742
- 部分背包问题(贪心算法)
- SAX解析出现断字符问题,只能获取部分字符
- 【算法导论】0-1背包问题 与 部分背包
- iOS BUG整理-应用登录时遇到的部分问题解析
- 高效算法设计_贪心法(最优装载问题,部分背包问题,乘船问题)
- 部分背包问题(贪心算法)
- android新浪微博开发-------授权部分实例+官方Api解析+可能遇到的问题
- 浏览器解析、加载、渲染+部分浏览器优化问题
- 贪心算法---0/1部分背包问题