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了… ...
随机推荐
- div,span,p等转换成可编辑
当前它能够将任意不可编辑的标签(span.div.p...等)转换成可编辑的text input.password.textarea.下拉列表(drop-down list)等标签.你可以利用它的ed ...
- AE,按照属性值关系选择要素
if(axMapControl2.LayerCount<=0) { MessageBox.Show("请加载图层后使用该功能","系统提示",Messag ...
- Autodesk 360 Mobile不能显示图片?
在6月21号的DevLab上,有一位朋友说Autodesk 360 Mobile在iPad上不能显示JPG图片预览.我当时没带iPad,不能测试.后天回家在Autodesk 360 Mobile 3. ...
- html框架—多对话框(相同id)处理
一个网站的数据大多数都是异步刷新的,这没什么好说的,然后现在很多前后端框架,大家都知道框架很好用,不用自己写样式,只要利用框架上的语法就能做出漂亮的动态的效果来,而用框架的话大多数的动态效果都是动态生 ...
- iOS设置文字过长时的显示格式
以label为例: //设置文字过长时的显示格式 aLabel.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间 aLabel.lineB ...
- UIApplication是什么
1.UIApplication对象是应用程序的象征 2.每一个应用都有自己的UIApplication对象,而且是单例的(只有一个) 3.通过[UIApplication sharedAppl ...
- Android 数据库的事务
什么是数据库的事务 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言书写的用户程序的执行所引起,并用形如begin ...
- iOS开发之邓白氏编码申请流程
要申请企业证书,必须先申请邓白氏编码,在苹果网站有一个免费申请邓白氏编码的链接:https://developer.apple.com/program/enroll/dunsLookupForm.ac ...
- ios git 终端提交
git status //检查提交状态 git status On branch master //检查分支 git branch //查看分支 git add * //添加所有本地更 ...
- 启用Mac(OS X Yosemite)自带的apache
刚用Mac的时候配置过一次Mac自带的apache,主要是平常自己用mackdown写文档,装成html文件放到apache下方便自己和同事阅读.后来升级各种东西,估计是升级OS X导致apache不 ...