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) ...
随机推荐
- jQuery为啥要提供一个load()方法?
上午的时候,找个闲暇事件整理之前整理的一些关于jQuery的东西,看到了一个之前做的jQuery的$(document).ready()与window.onload()方法的比較. 上面两个方法最重要 ...
- C++ Primer 学习笔记_76_模板和泛型编程 --模板定义[继续]
模板和泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- 阅读代码分析工具Understand 2.0试用
Understand 2.0是一款源码阅读分析软件,功能强大.试用过一段时间后,感觉相当不错,确实能够大大提高代码阅读效率. 因为Understand功能十分强大,本文不可能详尽地介绍它的全部功能,所 ...
- 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)
原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...
- 八皇后问题详细分析与解答(递归法解答,c#语言描述)
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...
- js中获取jsp中的参数
碰到一个问题需要再js中根据jsp中request的参数判断执行那段代码 第一种写法: if('${method}'=="add"){js代码段1}else{js代码段2} 第二种 ...
- Mojo 分析日志接口
#!/usr/bin/perl #取文件行数 ##循环开始清空文件 use POSIX; use DBI; my $dir = '/data01/applog_backup'; my $file = ...
- Java经典23种设计模式之创造型模式(一)
设计模式被称为程序猿的内功,之前零零散散的看过一大部分,但自己么有总结过.故此次在这里总结下.值得一提的是,设计模式并不是Java所特有.由于一直搞Android.这里就用Java为载体.最经典的设计 ...
- [Android]mac下开发环境搭建
好像没神马好些的? 1.下载adt-bundle-mac-x86_64bit(http://developer.android.com/sdk/installing/bundle.html) 2.解压 ...
- kill命令"-1"这个参数到底是杀进程还是reload?(转)
kill-1:重新读取一次参数的配置文件 (类似 reload) 这句话给我的感觉是把进程杀掉后重启进程,即 reload.而我查了下 man kill,-1 对应的 signal 是 SIGHUP, ...