Cod sursa(job #1320793)

Utilizator mariapascuMaria Pascu mariapascu Data 18 ianuarie 2015 15:15:12
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

#define INF 0x3f3f3f3f
#define Dim 10002

#define n G

int G, W;
int E[Dim], C[Dim], D[Dim/2];

void Read();
void Dyn();

int main()
{
    Read();
    Dyn();
    fin.close();
    fout.close();
    return 0;
}

void Dyn()
{
    for ( int i = 1; i <= W; ++i )
        D[i] = INF;

    for ( int i = 1; i <= n; ++i )
        for ( int j = W - E[i]; j >= 0; --j )
            //if ( j >= E[i] ) {
                if ( D[j + E[i]] > D[j] + C[i] )
                    D[j + E[i]] = D[j] + C[i];
              //  }
;

    if ( D[W] != INF )
        fout << D[W];
    else
        fout << -1;
}

void Read()
{
    fin >> G >> W;
    for ( int i = 1; i <= G; ++i )
        fin >> E[i] >> C[i];
}