I am trying this problem on SPOJ. Its basically a 0/1 knapsack.
Problem Statement: We are given N≤1000 choice of air tanks having oxygen capacity, nitrogen capacity, and weight O[i], N[i], and W[i] respectively O≤21,N≤79,W≤800. We need enough tanks to fuel t oxygen and a nitrogen respectively to complete the dive. Find the minimum total weight of tanks needed to complete the dive.
I already implemented a 3-D dynamic programming solution. But I am getting Wrong Answer. Please help.
Source Code:
int dp[1002][23][81]; // to store subproblems
int ox[1002],nt[1002],wt[1002]; // ox: oxygen; nt: nitrogen; wt = weight of cylinder
void solve(int n, int oxy, int nit){ // n: no. of cylinders, oxy: oxygen required; nit: nitrogen required
for(int i = 0; i <=n; ++i)
for(int j = 0; j <= oxy; ++j)
for(int k = 0; k <= nit; ++k){
if(i == 0)
dp[i][j][k] = INF;
else if(j == 0 && k == 0)
dp[i][j][k] = 0;
else
dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][max(j-ox[i], 0)][max(k-nt[i], 0)] + wt[i]);
}
printf("%d\n", dp[n][oxy][nit]);
}
Please help. Complete Source Code