poj1276 Cash Machine(多重背包)


http://poj.org/problem?id=1276

题意:银行里有一个取款机,cash表示应该取的钱数,接着有n种钞票,每种钞票的数量ni、面值di,求这个取款机最多可以取出多少钱。


思路:简单的多重背包。每种物品的花费和价值为同一属性。不要求装满背包。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

typedef long long LL;

const int N = 100005;
const int INF = 0x3f3f3f3f;

int dp[N], V;

void ZeroOnePack(int cost, int weight)
{
    for(int j = V; j >= cost; j--)
        dp[j] = max(dp[j], dp[j-cost]+weight);
}

void CompletePack(int cost, int weight)
{
    for(int j = cost; j <= V; j++)
        dp[j] = max(dp[j], dp[j-cost]+weight);
}

void MultiplePack(int cost, int weight, int amount)
{
    if(cost*amount>=V)
    {
        CompletePack(cost, weight);
        return;
    }
    int k = 1;
    while(k < amount)
    {
        ZeroOnePack(cost*k, weight*k);
        amount-=k;//注意在前面
        k=k*2;
    }
    ZeroOnePack(amount*cost, amount*weight);
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int n;
    int mon[N], amount[N];
    while(~scanf("%d%d", &V, &n))
    {
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &amount[i], &mon[i]);
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
            MultiplePack(mon[i], mon[i], amount[i]);
      /*  for(int i = 0; i <= V; i++)
            printf("%d ", dp[i]);
        printf("\n");*/
        printf("%d\n", dp[V]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值