Cod sursa(job #1276704)

Utilizator vlad.florescu94FMI Florescu Vlad - Adrian vlad.florescu94 Data 26 noiembrie 2014 19:19:24
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include<fstream>
#include<algorithm>

using namespace std;

class Object{
public:
    int weight;
    int profit;
    Object(int weight,int profit):profit(profit),weight(weight){}
    Object(const Object &copyO){
        this->weight = copyO.weight;
        this->profit = copyO.profit;
    }
};

#define NMAX 5000
#define GMAX 10000

Object *objects[NMAX];
int bestSol[GMAX+1];
int n,gMax;

int main(){

    ifstream f("rucsac.in");
    ofstream g("rucsac.out");

    f>>n>>gMax;
    int w,p;

    for(int i=0; i<n; ++i){

        f>>w>>p;
        objects[i] = new Object(w,p);
    }

    for(int i=0; i<n; ++i){
        for(int j=gMax; j>=0; --j)
            if(objects[i]->weight<= j){ ///daca pot adauga la solutii ob respectiv

                bestSol[j] = max(bestSol[j], bestSol[j- objects[i]->weight] + objects[i]->profit);
                ///cea mai buna solutie ori ramane cea care era deja ori devine
                /// solutia in care adaug profitul pt elementul asta unde pot (la greutatea asta - greutatea elementului asta)
        }
    }

    g<<bestSol[gMax];

    f.close();g.close();
    return 0;

}