POJ 1276 Cash Machine(多重背包)
|
Time Limit: 1000MS |
Memory Limit: 10000K |
|
|
Total Submissions: 24132 |
Accepted: 8446 |
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
题意, 给你最大的钱数 Case ,和 nk 种不同的面值, 每种面值D1...DN 对应的数量 n1....nN, 找出不超过最大钱数Case
思路:多重背包
import java.io.*;
import java.util.*;
/*
*
* author : deng_hui_long
* Date : 2013-8-31
*
*/
public class Main {
int Case,n;
int dp[]=new int[1000000];
public static void main(String[] args) {
new Main().work();
}
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
Case=sc.nextInt();
n=sc.nextInt();
if(n==0&&Case==0)
break;
Node node[]=new Node[n];
Arrays.fill(dp,0);
for(int i=0;i<n;i++){
int a=sc.nextInt();
int b=sc.nextInt();
node[i]=new Node(a,b);
}
for(int i=0;i<n;i++){
multiplePack(node[i].val,node[i].val,node[i].cost);
}
System.out.println(dp[Case]);
}
}
//多重背包
void multiplePack(int cost,int weight,int amount){
if(cost*amount>=Case)//超过最大的钱数,按完全背包处理
completePack(cost,weight);
else{//小于最大的钱数,按01背包处理
int k=1;
while(k<amount){
zeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;//左移一位,表示乘以2
}
zeroOnePack(amount*cost,amount*weight);
}
}
//完全背包
void completePack(int cost,int weight){
for(int i=cost;i<=Case;i++){
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
}
//01背包
void zeroOnePack(int cost,int weight){
for(int i=Case;i>=cost;i--)
dp[i]=Math.max(dp[i],dp[i-cost]+weight);
}
class Node{
int cost;
int val;
Node(int cost,int val){
this.cost=cost;
this.val=val;
}
}
}
POJ 1276 Cash Machine(多重背包)的更多相关文章
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- poj 1276 Cash Machine_多重背包
题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...
- 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- Cash Machine (POJ 1276)(多重背包——二进制优化)
链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
随机推荐
- 转载【浅谈ThreadPool 线程池】
浅谈ThreadPool 线程池 http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html
- 根据图像路径,创建CBitmap对象的方法
因为项目的关系,需要根据图像路径,创建CBitmap对象.起初查资料找到了LoadBitmap这个函数,根据CSDN得 BOOL LoadBitmap ( LPCTSTR lpszResourceNa ...
- mysql sql执行顺序
<pre name="code" class="html">mysql> explain select * from (select * fr ...
- jfinal框架教程-学习笔记
jfinal框架教程-学习笔记 JFinal 是基于 Java 语言的极速 WEB + ORM 开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restfu ...
- hdu2066一个人的旅行
枚举全部相邻城市,作为起点,多次spfa,然后每次在想去的城市中找出spfa后的距离起点最短的花费时间 #include <iostream> #include <cstring&g ...
- 一步步学习Linux开发环境搭建与使用
00.Linux开发环境搭建与使用1--Linux简史 01.Linux开发环境搭建与使用2--Linux系统(ubuntu)安装方案 02.Linux开发环境搭建与使用3--通过虚拟机安装系统(ub ...
- QLineEdit 自动完成(使用setCompleter,内含一个ListView)
-------------------------------------CompleteLineEdit.h------------------------------------- #ifndef ...
- 利用Gearman实现并发查询(Multi-Query)
这个样例是想从数据库查询出几个结果集,一般的做法是,一个接一个的发送查询,然后汇总结果进行输出. 以下我们利用Gearman的gearman_client_run_tasks实现并发的查询,gearm ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- asp.net出现正在中止线程解决方案
刚才又再次遇到了一个之前遇到的问题,在这里记录一下. 起因: 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAb ...