-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIGBANK.cpp
45 lines (42 loc) · 1.01 KB
/
PIGBANK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//CPP program for unbounded knapsack with minimization
#include<bits/stdc++.h>
using namespace std;
#define MX 1000000
//returns min value of knapsack with capacity w
void unboundedKnapsack_min(int w,int val[],int wt[],int n)
{
int dp[w+1],i,k;
//storing maximum value
for(i = 1; i < w + 1; i++)
dp[i] = MX;
dp[0] = 0;
for(i = 1; i <= w; i++)
{
for(k = 0; k < n; k++)
{
if(wt[k] <= i)
{
dp[i] = min( dp[i], dp[i-wt[k]]+val[k] );
}
}
}
if(dp[w]==MX)
cout<<"This is impossible.\n";
else
cout<<"The minimum amount of money in the piggy-bank is "<<dp[w]<<".\n";
}
//Driver program
int main()
{
int t,pig,bank,coin,i;
cin>>t;
while(t--)
{
cin>>pig>>bank>>coin;
int val[coin+1],wt[coin+1];
int w= bank-pig;
for(i=0;i<coin;i++)
cin>>val[i]>>wt[i];
unboundedKnapsack_min(w,val,wt,coin);
}
}