SCUBADIV - Scuba diver - SPOJ Solution C++

  Problem Link : SCUBADIV 


👉 Hint : Use 3D DP

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
long long int dp[1001][22][80];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int c;
    cin>>c;
    while(c--)
    {
        int n,t,a,o,nit;
        cin>>t>>a;
        cin>>n;
        vector<pair<int,int> >vol;
        int wt[n+1];
        vol.reserve(n+1);
        for(int i=1;i<=n;i++)
        {
            cin>>o>>nit>>wt[i];
            vol[i]=make_pair(o,nit);
        }
        memset(dp,0,sizeof(dp));
        int x,y;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=t;j++)
            {
                for(int k=0;k<=a;k++)
                {
                    if(j==0 && k==0)
                        continue;
                    if(i==1)
                    {
                       if(j<=vol[i].first && k<=vol[i].second)
                            dp[i][j][k]=wt[1];
                        else
                            dp[i][j][k]=INT_MAX; 
                    }
                    else
                    {
                    x=j-vol[i].first;
                    y=k-vol[i].second;
                    if(x<0)
                        x=0;
                    if(y<0)
                        y=0;
                    dp[i][j][k]=min(dp[i-1][j][k],wt[i]+dp[i-1][x][y]);
                    }
                }
            }
        }
        cout<<dp[n][t][a]<<endl;
    }
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!