Cod sursa(job #873918)

Utilizator bratiefanutBratie Fanut bratiefanut Data 7 februarie 2013 19:10:50
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
using namespace std;

ifstream cin("energii.in");
ofstream cout("energii.out");

int N, S, e[5001],c[5001], v[1001],G, cost[5001],minim=9999999;

int main()
{ int i,j,k;
    //N=g; S=w v=e cost=c
    cin>>N>>S;
    for(k=1;k<=N;k++)
    cin>>v[k]>>cost[k];

    for(j=1;j<=S;++j)
    c[j]=-1; c[0]=0;

    for (i=1; i<=N; ++i)
        for (j=S; j>=0; --j)
            if (c[j] != -1)
            {
                if (j + v[i] > S)
                    if (minim > c[j] + cost[i])
                        minim = c[j] + cost[i];
                if (j + v[i] <= S)
                    if (c[j + v[i]] > c[j] + cost[i] || c[j + v[i]] == -1)
                        c[j + v[i]] = c[j] + cost[i];
            }
    if(c[S]=-1 || minim==9999999)
    cout<<-1;
    else
    cout<<minim;
    return 0;
}