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了… ...
随机推荐
- JavaScript学习笔记-setTimeout应用
setTimeout应用 var ids = [];function foo1(i) { this.i = i; console.log('i = '+i); ids[0] = setTimeout( ...
- cell重用的几种方式
1.使用xib重用 //ios6 之后推荐大家使用的重用方式 //动态的使用self获得当前类名,来作为唯一的标示 NSString * identifier = NSStringFromClass( ...
- ABAP 动态生成内表的几种方法
最近要写个程序,既有更新的,也有删除的,需要涉及到很多系统表,如果一个表一个表进行更新或者删除太慢了,于是就想通过创建动态内表来实现这些功能,在网上找了一些资料,经过多次尝试,终于测试成功了.网上讲述 ...
- Oracle中用随机数更新字段----将一张表的数据插入另一张表----环境设置
DECLARE CURSOR recordCursor IS SELECT longitude,latitude FROM WR_WIUST_B_SEC FOR UPDATE; recordRow r ...
- 给SHP文件定义投影
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import os def get_epsg_code(epsg ...
- linux集群运维工具:clustershell和pssh
由于需要安装hadoop集群,有10台机器需要安装,一开始打算用SCP复制,后来觉得不可接受(实际现场可能数倍的机器集群,就是10台也不想干).后来在网上找了,发现了clustershell和pssh ...
- [Android]AndroidInject增加sqlite3数据库映射注解(ORM)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3623050.html AndroidInject项目是我写的一 ...
- 【代码笔记】iOS-点击加号增加书架,点击减号减少书架
一,效果图. 二,工程图. 三,代码. ReaderViewController.h #import <UIKit/UIKit.h> @interface ReaderViewContro ...
- 【VLC-Android】vlc-android简例
前言 继续折腾vlc,做这个例子并不顺利,卡在只有声音没有图像这个问题好久,网上的例子有些API已经对不上,继续分享,,, 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cn ...
- UISegmentedControl(人物简介)
效果图 当你点击上面人物名字的时候 ,就可以随意切换人物. 这个很有趣 , 你还可以试着添加音乐播放器 .以及一些别的来完善你想做的. 好吧 , 废话不多说 , 上代码. #import " ...