背包问题模板,POJ(1014)
题目链接:http://poj.org/problem?id=1014
背包问题太经典了,之前的一篇博客已经讲了背包问题的原理。
这一个题目是多重背包,但是之前的枚举是超时的,这里采用二进制优化。
这是所有01背包,完全背包,多重背包的模板哦!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int sum;
int num[], dp[ + ]; void ZeroOnePack(int cost, int weight, int V)
{
for (int i = V; i >= cost; i--)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} void CompletePack(int cost, int weight, int V)
{
for (int i = cost; i <= V; i++)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} void MultiPack(int cost, int weight, int V, int amount)
{
if (cost * amount >= V)
{
CompletePack(cost, weight, V);
return;
}
int k = ;
while (k < amount)
{
ZeroOnePack(cost * k, weight * k, V);
amount -= k;
k *=;
}
ZeroOnePack(cost * amount, weight * amount, V);
} int main()
{
int t = ;
while (~scanf("%d", &num[]))
{
sum = num[];
for (int i = ; i <= ; i++)
{
scanf("%d", &num[i]);
sum += num[i] * i;
}
if (num[] + num[] + num[] + num[] + num[] + num[] == ) break;
printf("Collection #%d:\n", t++);
if (sum % )
{
puts("Can't be divided.\n");
continue;
}
sum >>= ;
memset(dp, , sizeof(dp));
for (int i = ; i <= ; i++)
{
MultiPack(i, i, sum, num[i]);
}
if (dp[sum] != sum)
{
puts("Can't be divided.\n");
}
else
puts("Can be divided.\n");
}
return ;
}
背包问题模板,POJ(1014)的更多相关文章
- 证明 poj 1014 模优化修剪,部分递归 有错误
这个问题是存在做.我发现即使是可行的一个问题,但不一定正确. 大部分数据疲软,因为主题. id=1014">poj 1014 Dividing 题目大意:有6堆石头,权重分别为1 2 ...
- POJ-动态规划-背包问题模板
背包问题模板 一.0-1背包 状态:背包容量为j时,求前i个物品所能达到最大价值,设为dp[i][j].初始时,dp[0][j](0<=j<=V)为0,没有物品也就没有价值. 状态转移方程 ...
- 【多重背包模板】poj 1014
#include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- 最小费用最大流模板 poj 2159 模板水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15944 Accepted: 8167 Descr ...
随机推荐
- 缓冲区 粘包 029 send 和sendall 的区别 find 和 findall 的区别
一.tcp : 属于长连接 与客户端连接了之后 其他客户端需要等待 要连接另外一个 必须优雅的断开前面这个客户的连接. 二.缓冲区 :为了避免网络传输信号不通畅而是程序一直停留在消息发送状态而不向下进 ...
- C++ opencv 滑动条 Trackbary以及处理三通道和单通道图像
#include <opencv2\core.hpp> #include <opencv2\highgui.hpp> #include <opencv2\imgproc. ...
- ubuntu 修改hostname
1.sudo gedit /etc/hostname 2. 修改成你的新名字,例如 SS1 3. 保存,退出 3. sudo gedit /etc/hosts 4修改成心的名字 SS1 5. 保存,退 ...
- maya怎样卸载干净
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- Transform控制的物体移动
Transform控制的物体移动:public class TKMove : MonoBehaviour { public float HSpeed; public float VSpeed; pri ...
- node+mongoDB+express项目需求解释
1. morgon模块 --- morgon 用于打印日志,分别为向后台打印和向文件中打印两种情况.stackoverflow. 2. app.use(bodyParser.json()) 3. de ...
- javaScript 删除确认实现方法总结分享
第一种方法:挺好用的,确认以后才能打开下载地址页面.原理也比较清晰.主要用于删除单条信息确认.<SCRIPT LANGUAGE=javascript> function p_del() { ...
- Flyweight_pattern--reference
http://en.wikipedia.org/wiki/Flyweight_pattern In computer programming, flyweight is a software desi ...
- 用 JS 做一个数独游戏(一)
用 JS 做一个数独游戏(一) 数独的棋盘由 9x9 的方格组成,每一行的数字包含 1 ~ 9 九个数字,并且每一列包含 1 ~ 9 这 9 个不重复的数字,另外,整个棋盘分为 9 个 3x3 的块, ...
- 前端如何做好SEO优化
https://www.cnblogs.com/weiyf/p/9511021.html 一:什么是SEO? 搜索引擎优化(Search Engine Optimization),简称SEO.是按照搜 ...