Cod sursa(job #958205)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 7 iunie 2013 09:56:18
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <limits>

#define MAX_WATTS 5002
#define MAX_GEN 1002

using namespace std;

int energy[2][MAX_WATTS];

int main()
{
    int w, g;
    int maxPrice = numeric_limits<int>::max() / 2;
    pair<int, int> generators[MAX_GEN];

    fstream fin("energii.in", fstream::in);
    fstream fout("energii.out", fstream::out);
    
    fin >> g >> w;
    //cout << g << " " << w << endl;
    
    for (int i=0; i<g; ++i)
    {
        fin >> generators[i].first >> generators[i].second;
    }
    
    //sort(generators, generators + g);
    
    fill_n(energy[0], w + 1, maxPrice);
    fill_n(energy[1], w + 1, maxPrice);

    int current = 0;
    
    energy[0][0] = energy[1][0] = 1;
    for (int i=0; i<g; ++i)
    {
        //cout << generators[i].first << " " << generators[i].second << endl;
        for (int j=0; j<w; ++j)
        {
            int energyIndex = min(w, j + generators[i].first);
            energy[current][j] = min(energy[current][j], energy[!current][j]);
            energy[current][energyIndex] = min(energy[current][energyIndex], energy[!current][energyIndex]);

            if (energy[current][j] < maxPrice)
            {
                energy[current][energyIndex] = min(energy[current][energyIndex], generators[i].second + energy[!current][j]);
            }
        }
        
        /*ostream_iterator<int> itOut(cout, " ");
        copy_n(energy[current], w + 1, itOut);
        cout << endl;*/

        //copy_n(energy[current], w + 1, energy[!current]);
        current = !current;
    }
    
    if (energy[!current][w] < maxPrice)
    {
        fout << energy[!current][w] - 1 << "\n";
        return 0;
    }

    fout << -1 << "\n";
    
    return 0;
}