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了… ...
随机推荐
- SharePoint2013 错误
英文错误信息: This operation can be performed only on a computer that is joined to a server farm by users ...
- RapidFloatingActionButton框架正式出炉
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4474748.html RapidFloatingActionB ...
- Android 利用内容观察者实现短信窃听
<Android 内容观察者的原理>中介绍了内容观察者的一些基本原理,并做了简单的实战,本文接着进一步做一个小项目实战 package com.wuyudong.smslistener; ...
- android 界面设计基本知识Ⅱ
上一章讲述了Android界面设计时,一些基本控件的使用,本章主要讲述自定义控件,Fragment和Headler线程机制. 1.自定义控件 (1)基本知识 dp.sp和dx px:像素点 ...
- iOS 学习 - 10下载(2) NSURLSession 图片 篇
使用NSURLSessionDownloadTask下载文件的过程与前面差不多,需要注意的是文件下载文件之后会自动保存到一个临时目录,需要开发人员自己将此文件重新放到其他指定的目录中. // // V ...
- JAVA同步容器和并发容器
同步容器类 同步容器类的创建 在早期的JDK中,有两种现成的实现,Vector和Hashtable,可以直接new对象获取: 在JDK1.2中,引入了同步封装类,可以由Collections.sync ...
- Hello BIEE
这篇文章提供了一个Hello World式的例子,讲述如何创建一个最简单的BIEE资料库.本文使用的示例数据可以在从此链接下载:http://www.zw1840.com . 目录 创建资料库 创 ...
- view组件的duplicateParentState属性
今天做页面的时候遇到一个小问题,在点击的时候改变组件的图片来源,这个很简单大家都知道,用selector可以很快实现.但是现状有点特殊,是 LinearLayout 中包裹着一个 ImageView ...
- HTML基础(五)——-css样式表——样式属性——格式与布局
一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例: 二.position:absolute 绝对位置: 1.外层没有position:a ...
- MySQL数据类型——数值类型
1.1.1 整型 整型 占用字节 范围 范围 tinyint 1 -27~27-1 -128~127 smallint 2 -215~215-1 -32768~32767 mediumint 3 -2 ...