poj1014 多重背包
来源:互联网 发布:dota2 mac 下载 编辑:程序博客网 时间:2024/06/06 02:59
题目链接:点击打开链接
题意:
与hdu1059的题一模一样;
点击打开链接
理解:
多重背包;
看做01背包会超时;
直接套模板;
与《挑战程序设计》书上多重背包是一样的;
递推式:dp[i] = max(dp[i], dp[i - (i + 1) * mul] + (i + 1) * mul);
其中的值根据程序而定;
具体看hdu1059题解;
但是写法不一样;
代码如下:
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <map>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;typedef pair<int, int> PII;const int MIN_INF = 1e-7;const int MAX_INF = (1e9) + 7;#define X first#define Y secondint dp[555555];int main() { for (int I = 1; I > 0; ++I) { memset(dp, 0, sizeof(dp)); //打脸,竟然会Wrong Answer在初始化; int a[6]; int t = 0, sum = 0; for (int i = 0; i < 6; ++i) { cin >> a[i]; t += a[i]; sum += a[i] * (i + 1); } if (t == 0) { break; } for (int i = 0; i < 6; ++i) { int num = a[i]; for (int k = 1; num > 0; k <<= 1) { int mul = min(k, num); for (int j = sum / 2; j >= mul * (i + 1); --j) { dp[j] = max(dp[j], dp[j - (i + 1) * mul] + (i + 1) * mul); } num -= mul; } } cout << "Collection #" << I << ":" << endl; if (dp[sum / 2] == sum - sum / 2) { cout << "Can be divided." << endl; } else { cout << "Can't be divided." << endl; } cout << endl; } return 0;}
0 0
- POJ1014 - 多重背包
- poj1014 多重背包
- POJ1014--多重背包--Dividing
- 多重背包--poj1014
- poj1014 多重背包
- POJ1014 多重背包问题
- poj1014 多重背包
- poj1014多重背包
- POJ1014 多重背包
- poj1014(多重背包)
- poj1014 多重背包
- poj1014多重背包
- POJ1014 Dividing 多重背包[DP]
- HDU1059 && POJ1014 :Dividing(多重背包)
- hdu1059 &poj1014 Dividing (多重背包)
- poj1014 hdu1059 Dividing 多重背包
- poj1014 Dividing (多重背包)
- poj1014 Dividing(递归,多重背包)
- 想一想你想成为一个什么样的人
- 特殊回文数
- jQuery
- 源码下载--ubuntu系统中下载安卓源码
- 浅析extern “C”的作用
- poj1014 多重背包
- 对象 Console
- 最长下降子序列 + 最长的方案数
- jQuery源码学习 item1-匿名函数及其参数
- jQuery
- uip1.0 学习笔记--NUC472
- FastDFS分布式文件系统集群安装
- JS
- LabVIEW实现2048小游戏