Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

Knapsack problem

$
0
0

Hey I just learnt the knapsack algo and tried to solve http://www.codechef.com/problems/PPTEST/ but its showing WA. This is my code

#include <iostream>

using namespace std;

int main() {

int t;
cin>>t;
while(t--)
{
 int time[110];
 int value[110];
 int W,N;
 cin>>N>>W;
 for(int h=1;h<=N;h++)
 { 
    int t1,t2;
    cin>>t1>>t2>>time[h];
    value[h]=t1*t2;
 }  
 int a[100][100];       //value,time
 for(int l=0;l<N;l++)
 {
    a[l][0]=0;      
 }

 for(int k=0;k<W;k++)
 {
    a[0][k]=0;      
 }
for(int i=1;i<=N;i++)
{
    for(int y=1;y<=W;y++)
    {
        if(time[i]>y)
        {
            a[i][y]=a[i-1][y];
        }else
        {
            a[i][y]=max(a[i-1][y],(a[i-1][y-time[i]]+value[i]));
        }
    }
}
cout<<a[N][W];  
}
return 0;

}


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>