Pagini recente » Cod sursa (job #545374) | Cod sursa (job #2449594) | Cod sursa (job #1974810) | Cod sursa (job #1761993) | Cod sursa (job #2626660)
#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][1]; curWeight>=0; --curWeight){
if( dpVal[curWeight+objects[i][0]] < dpVal[curWeight] + objects[i][1] )
{
dpVal[curWeight+objects[i][1]] = dpVal[curWeight] + objects[i][0];
maximum = std::max(dpVal[curWeight+objects[i][1]],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);
}