Cod sursa(job #1910797)

Utilizator andistroieAlexandru-Mihai Stroie andistroie Data 7 martie 2017 18:20:28
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int bpSum(vector<int> val, vector<int> wt, int W) {

    int n = val.size() ;
    int mat[n+1][W+1];

    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= W; j++) {
            if (i == 0 || j == 0) {
                mat[i][j] = 0;
            } else if (j <= wt[i-1]) {
                mat[i][j] = mat[i-1][j];
            } else {
                mat[i][j] = max( (val[i] + mat[i-1][j - wt[i]]), mat[i-1][j] );
            }
        }
    }

    return mat[n][W];
}

int main()
{
    int n,x,W;
    vector<int> val, wt;
    ifstream f ("rucsac.in");
    //cin>>n >> W;
    f>> n >> W;
    //cout << n << " " << W;
    for(int i = 0; i < n; i++) {
        f >> x;
        wt.push_back(x);
        f >> x;
        val.push_back(x);
    }
    f.close();
    /*for(int i = 0; i < n; i++) {
            cout << val[i] << " ";
    }
    cout << endl;
     for(int i = 0; i < n; i++) {
            cout << wt[i] << " ";
    }
    cout << endl;*/
    //cout << bpSum(val,wt,W) << endl;
    ofstream g ("rucsac.out");
    g << bpSum(val,wt,W);
    g.close();
    return 0;
}