POJ1276Cash Machine[多重背包可行性]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32971 | Accepted: 11950 |
Description
N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10
means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.
Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.
Notes:
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.
Input
cash N n1 D1 n2 D2 ... nN DN
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct.
Output
Sample Input
735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
Sample Output
735
630
0
0
Hint
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.
Source
// poj1276
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=,V=1e5+;
int n,cash,f[V],c[N],w[N],v[N]; inline void zp(int v,int w){
for(int i=cash;i>=v;i--)
//f[i]=max(f[i],f[i-v]+w);
f[i]=f[i]|f[i-v];
}
inline void cp(int v,int w){
for(int i=v;i<=cash;i++)
// f[i]=max(f[i],f[i-v]+w);
f[i]=f[i]|f[i-v];
}
inline void mp(int v,int w,int c){
if(c*v>=cash){cp(v,w);return;}
int k=;
while(k<c){
zp(k*v,k*c);
c-=k;
k*=;
}
zp(v*c,w*c);
} int main(int argc, const char * argv[]) {
while(cin>>cash>>n){
f[]=;
memset(f,,sizeof(f)); f[]=;
for(int i=;i<=n;i++){
cin>>c[i]>>v[i];
mp(v[i],w[i],c[i]);
}
for(int i=cash;i>=;i--)if(f[i]>){cout<<i<<"\n";break;}
}
return ;
}
法2:O(NV) 然而却比法1慢...............
f[i][j]表示前i种物品装满容量为j的背包后还剩下几个i,不可行为-1
转移先通过f[i-1][j]判断可不可行,再用可行的f[i][j]更新f[i][j+vi](其实就是一遍完全背包)
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=,V=1e5+;
int n,cash,f[N][V],c[N],v[N]; void mpAble(){
memset(f,-,sizeof(f));
f[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=cash;j++){
if(f[i-][j]>=) f[i][j]=c[i];
else f[i][j]=-;
}
for(int j=;j<=cash-v[i];j++)
if(f[i][j]>=)
f[i][j+v[i]]=max(f[i][j+v[i]],f[i][j]-);
}
}
int main(int argc, const char * argv[]) {
while(cin>>cash>>n){
for(int i=;i<=n;i++) cin>>c[i]>>v[i];
mpAble();
for(int i=cash;i>=;i--)if(f[n][i]>=){cout<<i<<"\n";break;}
}
return ;
}
POJ1276Cash Machine[多重背包可行性]的更多相关文章
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
- POJ-1276 Cash Machine 多重背包 二进制优化
题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
随机推荐
- Jquery属性获取——attr()与prop()
今天在项目中使用<select></select>下拉菜单时,使用juery操作,使页面加载完菜单默认选中的值为2,我一开始的操作如下: <!--html部分--> ...
- 简单代码在ABAP中实现声音的播放
这段代码的功能是在SAP里面实现声音的播放,可以用作程序提醒功能,和SAP里面’噹噹噹’那个声音的意思差不多.将来在项目中遇到客户想要SAP ABAP发出一点声音的时候就可以参考一下这个程序. REP ...
- SharePoint Online 创建门户网站系列之首页布局
前 言 SharePoint Online中创建首页布局,一般都是首先将美工提供的效果图,切图成为Html + Css + Script的形式,然后,将所有资源文件传到SharePoint Onlin ...
- myeclipse 2015 CI 16发布【附下载】
2015升级版再次来袭! 更新日志: Slack Integration 新版本集成了Slack,你只需要注册一个Slack帐号然后就可以发送和接收代码片段.你甚至不需要登录Slack就可以直接在Ec ...
- [android]亲自破解Flappy Bird(去广告+永生)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3544785.html 听说最近Flappy Bird很火,但 ...
- 转:JS获取浏览器高度和宽度
发现一篇好文章,汇总到自己的网站上. IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> ...
- mysql中数据类型的取值范围
mysql整型bigint.int.mediumint.smallint 和 tinyint的语法介绍,如下: 1.bigint 从 -2^63 (-9223372036854775808) 到 2^ ...
- 1.5 基础知识——GP2.3 提供资源(Resources) 与 GP2.4 分配职责(Responisbility)
摘要: 没有资源和落实权责,将无法做好事情,这是很多公司很多人都懂的道理.但很多做CMMI改进的公司,号称很多核心人员负责过程改进,其实是兼职挂牌而已,有些甚至招聘应届生作为过程改进的主力…… 如此这 ...
- spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法
修改了 spring web 项目的 context root 为 / 之后,在启动项目时,会导致 WebApplicationContext 初始化两次,下面是其初始化日志: 第一次初始化: 四月 ...
- js中的三个编码函数:escape,encodeURI,encodeURIComponent
1. eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / .其他所有的字符都会被转义序列替换.其它情况下es ...