poj 1276 Cash Machine(多重背包求最大容量)
2016-10-26 21:50
176 查看
Cash Machine
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Submit Status
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
Sample Output
Hint
The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver
the exact amount of requested cash.
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered
cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.
01背包的变体,多重背包模板+中间记录一下就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100010;
const int inf = 999999999;
int dp
, v
, num
, w
;
int main()
{
int m, n;
while(scanf("%d %d", &m, &n)!=EOF)
{
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d %d", &num[i], &v[i]);
}
dp[0]=0;
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<=N;j++)
{
if(dp[j]>=0)
dp[j]=num[i];
else if(j<v[i]||dp[j-v[i]]<=0)
dp[j]=-1;
else
dp[j]=dp[j-v[i]]-1;
if(dp[j]>=0&&j>=sum&&j<=m)
sum=j;
}
}
printf("%d\n",sum);
}
return 0;
}
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Submit Status
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
Hint
The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver
the exact amount of requested cash.
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered
cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.
01背包的变体,多重背包模板+中间记录一下就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100010;
const int inf = 999999999;
int dp
, v
, num
, w
;
int main()
{
int m, n;
while(scanf("%d %d", &m, &n)!=EOF)
{
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d %d", &num[i], &v[i]);
}
dp[0]=0;
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<=N;j++)
{
if(dp[j]>=0)
dp[j]=num[i];
else if(j<v[i]||dp[j-v[i]]<=0)
dp[j]=-1;
else
dp[j]=dp[j-v[i]]-1;
if(dp[j]>=0&&j>=sum&&j<=m)
sum=j;
}
}
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- 容量有上下界的最大流 poj 2396 pku
- POJ 2063 Investment(完全背包--容量变化)
- dp -- 多重背包 [POJ-1276] 重量等于体积且背包容量较大时的算法
- 动态规划-背包问题(给定容量获得最大的价值量)1
- poj3093(背包最大容量)
- poj 3498 March of the Penguins 带点容量限制的做法是拆点,把一个点拆成两个点,他们直接连一条边就是点容量。然后再求最大流
- poj 2396 Budget 边容量有上下界的最大流
- 动态规划-背包问题(给定容量获得最大的价值量)2
- poj 2396 Budget 边容量有上下界的最大流
- (hdu step 3.3.1)Big Event in HDU(01背包:N件物品放在容量为V的背包中,第i件物品的费用是c[i],价值是w[i]。问所能获取的最大价值)
- 动态规划问题 -- 背包问题 单价不等水果 装入固定容量背包 最大价值问题
- hdu 2126 Buy the souvenirs(01背包求最大容量方法数)
- POJ 2987 Firing【最大权闭合图】
- Graph Coloring&&http://poj.org/problem?id=1419最大团问题
- poj 3181 Dollar Dayz(完全背包)
- POJ 2229 Sumsets——完全背包
- poj 2229 完全背包变形(求解整数拆分问题)
- poj Dollar Dayz 3181 (DP&完全背包) 好题
- poj 3084 dinic最大流
- POJ - 1252 Euro Efficiency(正负背包)