Pagini recente » Cod sursa (job #432607) | tema | Cod sursa (job #1252311) | Cod sursa (job #556758) | Cod sursa (job #2626662)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int knapSack(vector<vector<int>> objects, int maxWeight){
vector< int > dpVal(maxWeight+1);
int maximum = 0;
for(int i=1; i<objects.size(); ++i){
for(int curWeight = maxWeight-objects[i][0]; curWeight>=0; --curWeight){
if( dpVal[curWeight+objects[i][0]] < dpVal[curWeight] + objects[i][1] )
{
dpVal[curWeight+objects[i][0]] = dpVal[curWeight] + objects[i][1];
maximum = std::max(dpVal[curWeight+objects[i][0]],maximum);
}
}
}
return maximum;
}
int main()
{
int noObjects, maxWeight;
f>>noObjects>>maxWeight;
vector< vector<int> > obj(noObjects+1,vector<int>(2));
for(int i=1; i<=noObjects; ++i){
int val, weight;
f>>weight>>val;
obj[i][0] = weight;
obj[i][1] = val;
}
g<<knapSack(obj, maxWeight);
}