poj1276——dp,多重背包

poj1276——dp,多重背包

Cash Machine

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28826   Accepted: 10310

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题意:给若干种面值的纸币,第i种纸币有n[i]张,每张面值为D[i],问拼凑出不超过cash的最大面值是多少思路:很明显这是一个多重背包(每种纸币的数量是有限的),用二进制优化并转化为01背包解决,如果某种纸币的可拼凑出的面值超过了cash,则看成其数量是无限的,当成完全背包处理,反之则是有限的,用二进制优化当成01背包处理

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int maxn=1000100;
int cash,N;
int n[maxn],D[maxn];
int dp[maxn];

void ZeroOnePack(int weight,int val)
{
    for(int i=cash;i>=weight;i--){
        dp[i]=max(dp[i],dp[i-weight]+val);
    }
}

void CompletePack(int weight,int val)
{
    for(int i=weight;i<=cash;i++){
        dp[i]=max(dp[i],dp[i-weight]+val);
    }
}

void MultiPack(int weight,int val,int amount)
{
    if(weight*amount>=cash) CompletePack(weight,val);//如果超过限制,当完全背包处理
    else{                //否则用二进制优化并转为01背包处理
        int k=1;
        while(k<amount){//按k=1,2,4,...的顺序分解amount,对分解后的部分按01背包处理
            ZeroOnePack(k*weight,k*val);
            amount-=k;
            k*=2;
        }
        ZeroOnePack(amount*weight,amount*val); //剩下部分也按01背包处理
    }
}

int main()
{
    while(cin>>cash>>N){
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=N;i++){
            cin>>n[i]>>D[i];
            MultiPack(D[i],D[i],n[i]);
        }
        cout<<dp[cash]<<endl;
    }
    return 0;
}

时间: 2024-12-28 20:29:44

poj1276——dp,多重背包的相关文章

poj1014 dp 多重背包

1 //Accepted 624 KB 16 ms 2 //dp 背包 多重背包 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 120005; 8 int f[imax_n]; 9 int amount[7]; 10 int v; 11 int n=6; 12 int max(int a,int b) 1

ZOJ1366经典dp(多重背包转01背包+优化空间)

1 //zoj1366类似背包的问题 2 //争取一遍AC 3 #include<iostream> 4 #include<string.h> 5 #include<stdio.h> 6 #define maxn 13 7 using namespace std; 8 9 int k[maxn]; 10 int n1[maxn]; 11 int c1[maxn]; 12 int c2[105]; 13 int dp[110000]; 14 int Cash,N1,N2;

hdu1059 dp(多重背包二进制优化)

hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听过```不得不吐槽自己的知识面太窄```于是,我用了母函数写这题,母函数的做法并没有问题,但是由于这道题的数据很大,母函数轻轻松松就超时了,于是,我又很努力地在母函数循环的优化上面想出路,改改改,各种改之后依旧TLE,01背包的做法显然也是会超时的,DISCUSS里的母函数做法优化方式都是模上一个大

ACM学习历程—HDU 1059 Dividing(dp &amp;&amp; 多重背包)

Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just spl

POJ 1742 Coins ( 经典多重部分和问题 &amp;&amp; DP || 多重背包 )

题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到DP数组的定义 ==> dp[i][j] 表示使用前 i 种硬币是否可以凑成面额 j . 根据这样的定义,则一开始初始化 dp[0][0] = true 最后统计 dp[n][1 ~ m] 为 true 的数量即为答案 状态转移方程为 dp[i][j] |= dp[i-1][ j - k*val[i

CodeForces922E DP//多重背包的二进制优化

https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法上限会增加B 从一棵树走到另一棵树,会增加魔法X 一开始的魔法和魔法上限都是W 问最多能够召唤的鸟的个数 显然这是一道DP题 用dp[i][j]来表示到j这个树下选到j只鸟可以获得的最大能量值 很容易得出dp状态转移方程dp[i][j] = max(dp[i][j],dp[i][j - 1] - c

【DP|多重背包可行性】POJ-1014 Dividing

Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had

NOIP2012pj摆花[DP 多重背包方案数]

题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过ai盆,摆花时同一种花放在一起,且不同种类的花需按标号的从小到大的顺序依次摆列. 试编程计算,一共有多少种不同的摆花方案. 输入输出格式 输入格式: 第一行包含两个正整数n和m,中间用一个空格隔开. 第二行有n个整数,每两个整数之间用一个空格隔开,依次表示a1.a2.……an. 输出格式: 输出只有一行,一个整数

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers