zoukankan      html  css  js  c++  java
  • POJ 1276  Cash Machine(多重背包)

    Cash Machine

    Time Limit: 1000MS

     

    Memory Limit: 10000K

    Total Submissions: 24132

     

    Accepted: 8446

    Description

    A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example,

    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

    The program input is from standard input. Each data set in the input stands for a particular transaction and has the format:

    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

    For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.

    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;
    		}
    	}
    }
    
  • 相关阅读:
    基于layui的简易后台管理系统,后期会更新
    java基础语法知识(笔记)——(一)基本变量类型,循环,日期类
    JavaWeb学习总结(笔记)——SSM spring.xml的基本配置
    JavaWeb学习总结(笔记)——SSM中Web.xml文件
    JavaWeb学习总结(笔记)——SSM关键类及其作用
    JavaWeb学习总结(笔记)——关于SSM CRUD上传功能的总结
    JavaWeb——MVC与三层结构
    jquery结合highcharts插件显示实时数据动态曲线图
    jquery结合Highcharts插件实现动态数据仪表盘图形化显示效果
    在IE6/7下表格td标签没有内容时不显示边框?
  • 原文地址:https://www.cnblogs.com/riskyer/p/3293617.html
Copyright © 2011-2022 走看看