Cash Machine(多重背包)
2024-10-21 10:21:03
http://poj.org/problem?id=1276
#include <stdio.h>
#include <string.h>
const int N=;
#define Max(a,b) (a)>(b)?(a):(b)
int dp[N],cash;
void ZeroOnePack(int cost)//01背包
{
for (int i = cash; i >= cost; i--)
{
dp[i] = Max(dp[i],dp[i-cost]+cost);
}
}
void ComplexPack(int cost)//完全背包
{
for (int i = cost; i <= cash; i++)
{
dp[i] = Max(dp[i],dp[i-cost]+cost);
}
}
void MultiplePack(int cnt,int cost)//多重背包
{
if (cnt*cost > cash)
ComplexPack(cost);
else
{
int k = ;
while(k < cnt)
{
ZeroOnePack(k*cost);
cnt-=k;
k<<=;
}
ZeroOnePack(cnt*cost);
}
}
int main()
{
int n,cost[],cnt[];
while(~scanf("%d %d",&cash,&n))
{
for (int i = ; i <= n; i++)
{
scanf("%d %d",&cnt[i],&cost[i]);
}
memset(dp,,sizeof(dp));
for (int i = ; i <= n; i++)
{
MultiplePack(cnt[i],cost[i]);
}
printf("%d\n",dp[cash]);
}
return ;
}
最新文章
- Java数据结构——平衡二叉树的平衡因子(转自牛客网)
- LeetCode之387. First Unique Character in a String
- HIVE: Transform应用实例
- HTML布局篇之双飞翼(圣杯)布局
- Visual Studio 2010 更新NuGet Package Manager出错解决办法
- python练习程序(c100经典例6)
- IDS 日志分析
- Layout( 布局)
- Linux中yum的安装
- jQuery 方法
- 解题(DirGraCheckPath--有向图的遍历(深度搜索))
- ASP.Net Core2.1中的HttpClientFactory系列二:集成Polly处理瞬态故障
- nginx之快速查找配置文件
- Ubuntu下安装Goldendict(翻译软件)
- Spring Security的几个重要词
- Tensorflow Seq2seq attention decode解析
- Neural Networks and Deep Learning(week4)Building your Deep Neural Network: Step by Step
- sudo控制权限简单用法介绍
- Django商城项目笔记No.2项目准备工作
- Spring 学习02