题目:http://poj.org/problem?id=1276

多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int V,n,w[],cnt[],dp[];
void com(int v)
{
for(int i=v; i<=V; i++)
{
dp[i]=max(dp[i],dp[i-v]+v);
}
return ;
}
void one(int v)
{
for(int i=V;i>=v;i--)
{
dp[i]=max(dp[i],dp[i-v]+v);
}
return ;
}
int main()
{
while(scanf("%d",&V)!=EOF)
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%d%d",&cnt[i],&w[i]);
}
memset(dp,,sizeof(dp));
for(int i=; i<n; i++)
{
if(cnt[i]==) continue;
if(cnt[i]*w[i]>=V)
{
com(w[i]);
}
else
{
int t=;
while(t<cnt[i])
{
one(w[i]*t);
cnt[i]-=t;
t<<=;
}
one(w[i]*cnt[i]);
}
}
printf("%d\n",dp[V]);
}
return ;
}

POJ1276:Cash Machine(多重背包)的更多相关文章

  1. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

  2. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  3. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  6. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. PKU--1267 Cash Machine(多重背包)

    题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...

  9. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

随机推荐

  1. jQuery 时间戳转化成时间

    //时间转换  function   formatDate(now)   { var   now= new Date(now);      var   year=now.getFullYear();  ...

  2. spring的数据源

    Spring提供了两个这样的数据源(都位于org.springframework.jdbc.datasource程序包里):        DriverManagerDataSource:在每个连接请 ...

  3. IPC之SystemV

    svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...

  4. php 扩展模块添加

    1. 新增安装扩展模块的位置 [root@node_22 ~]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/ op ...

  5. VIM配置入门

    原文链接: http://www.ruanyifeng.com/blog/2018/09/vimrc.html 个人增加了两张收集来的图.

  6. 重载(Overload)

    重载(Overload) 重载(overloading) 是在一个类里面,方法名字相同,而参数不同.返回类型可以相同也可以不同. 每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表. 最 ...

  7. 抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面

    抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立 图形用户界面GUI (Graphics User Interface)工具集,AWT可用 ...

  8. 【vijos】1781 同余方程(拓展欧几里得)

    https://vijos.org/p/1781 学习了下拓欧.. 求exgcd时,因为 a*x1+b*y1=a*x2+b*y2=b*x2+(a-b*[a/b])*y2 然后移项得 a*x1+b*y1 ...

  9. Ubuntu使用yah3c连接校园网

    虽然网上有,但是我还是把这当作学习的一个过程记录下来,以备不时之需. 刚开始总是以为要上校园网就要用inode,所以总是百度谷歌不到方法,后来才知道yah3c不等同于inode,二者应该是并行的关系. ...

  10. 计算机视觉中的边缘检测Edge Detection in Computer Vision

    计算机视觉中的边缘检测   边缘检测是计算机视觉中最重要的概念之一.这是一个很直观的概念,在一个图像上运行图像检测应该只输出边缘,与素描比较相似.我的目标不仅是清晰地解释边缘检测是怎样工作的,同时也提 ...