Cod sursa(job #598864)

Utilizator deneoAdrian Craciun deneo Data 27 iunie 2011 13:54:15
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <cstring>
using namespace std;

int n, w, cost[5010], gen[1010][3];
bool fol[5010][1010];

int main()
{
    int i, j;

    ifstream f("energii.in");
    ofstream g("energii.out");
    f >> n >> w;
    for(i = 1; i <= n; ++i)
        f >> gen[i][1] >> gen[i][2];

    memset(cost, -1, sizeof(cost));
    cost[0] = 0;

    for(i = 1; i <= w; ++i)
        for(j = 1; j <= n; ++j)
            if(gen[j][1] <= i && cost[i - gen[j][1]] != -1 && !fol[i - gen[j][1]][j] && ((cost[i - gen[j][1]] + gen[j][2] < cost[i]) || cost[i] == -1))
            {
                memcpy(fol[i], fol[i - gen[j][1]], sizeof(fol[i]));
                fol[i][j] = 1;
                cost[i] = cost[i - gen[j][1]] + gen[j][2];
            }

    g << cost[w] << '\n';
    g.close();
    return 0;
}