背包问题模板,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 ...
随机推荐
- navicat premiun连接mysql数据库报错,错误代码:1251
今天在电脑上安装了Mysql 8.0.11,然后想用 Navicat Premium连接数据库,结果报错了: error 1251:client does not support authentica ...
- 汉诺塔问题hdu 2065——找规律
这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...
- python处理时间和日期
时间和日期 (图中错误修正:dt_obj.strftime(format)) import time, datetime 1. datetime obj 1) datetime dateti ...
- stringstream的使用
stringstream是 C++ 提供的另一个字串型的串流(stream)物件,和之前学过的iostream.fstream有类似的操作方式.要使用stringstream, 必须先加入这一行: # ...
- phpmyadmin 开放远程登录的权限
*linux下的修改* 在phpmyadmin.conf 加上如下试一下 <Directory "phpmyadmin路径"> AllowOverride No ...
- php和c++socket通讯(基于字节流,二进制)
研究了一下PHP和C++socket通讯,用C++作为服务器端,php作为客户端进行. socket通讯是基于协议的,因此,只要双方协议一致就行. 关于协议的选择:我看过网上大部分协议都是在应用层的协 ...
- Jersey初始化配置
一 实际项目配置 公司VIP平台因为业务的特殊性,对业务数据的操作.以及前后端解耦等要求,使用到了jersey框架.同时使用到了spring框架. 二 jersey初始化 配置web项目配置文件web ...
- java实现新旧版本号比较
项目中需要使用比较现在线上版本和新版本,然后新版本执行新方法,方法如下: /** * * @方法名称:comparaVersion * @内容摘要: <版本比较> * @param old ...
- C#实现Ping服务器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 在CentOS上配置MySQL服务
#!/bin/sh # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB # Thi ...