Cod sursa(job #787709)

Utilizator ichigo2908mantu radu ichigo2908 Data 13 septembrie 2012 19:56:37
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <fstream>
using namespace std;

typedef struct gen
{
	int putere, cost;
};

gen u[1001];
int v[5001], g, w;
const int INF=1000000000;

void init()
{
	for(int i=1;i<=w;i++)
		v[i]=INF;
}

int main()
{
	int i, j;

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

	in>>g>>w;
	for(i=0;i<g;i++)
		in>>u[i].putere>>u[i].cost;

	init();
	for(i=0;i<g;i++)
		for(j=w;j>=0;j--)
			if(v[i]!=INF && v[j]+u[i].cost<v[j+u[i].putere])
				v[j+u[i].putere]=v[j]+u[i].cost;
	for(i=w;v[i]==INF;i++)
		continue;

	out<<v[i];

	in.close();
	out.close();

	return 0;
}