title | comments | mathjax |
---|---|---|
Unbounded Knapsack (Minimization) for SPOJ-PIGBANK |
true |
true |
Given a knapsack weight W and a set of N items with certain value
We're given a box which can hold maximum W weight. There are N various coins each of value
100 ----- W
2 ------- N
1 1 ----- vi,wi
30 50 --- vi,wi
Answer of the problem above is, 60. If we take 2 coins of value 30, weight 50, we'll get exactly weight 100 with a minimum value of 60.
This is a DP solution. To get the solution for W=100, we'll solve subproblems first. Let dp[W+1] be an array which will store the subproblems solutions. In the end, dp[W] will give us the minimum amount. Subproblems soultion will be the minimum of taking the coin
#include<bits/stdc++.h>
using namespace std;
#define INTMAX 9999999
void unboundedKnapsack_min(int w,int val[],int wt[],int coin)
{
int dp[w+1],i,k;
for(i=1;i<w+1;i++)
dp[i] = INTMAX;
dp[0]=0;
for(i=1;i<=w;i++)
{
for(k=0;k<coin;k++)
{
if(wt[k]<=i)
{
dp[i] = min( dp[i], dp[i-wt[k]]+val[k] );
}
}
}
if(dp[w]==INTMAX) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[w]);
}
int main()
{
int t,pig,bank,coin,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&pig,&bank,&coin);
int val[coin+1],wt[coin+1];
int w= bank-pig;
for(i=0;i<coin;i++)
scanf("%d%d",&val[i],&wt[i]);
unboundedKnapsack_min(w,val,wt,coin);
}
}