Cod sursa(job #447089)

Utilizator sunmirrorSorin Stelian sunmirror Data 27 aprilie 2010 17:38:34
Problema Energii Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <stdio.h>

#define IN_FILE "energii.in"
#define OUT_FILE "energii.out"

#define MAX_G 1000
#define MAX_W 5000
#define MAX_COST 10001000

int G, W;
int EG[MAX_G], CG[MAX_G];

int main(void)
{
	int w, i;
	FILE *fin, *fout;
	int best_cost[MAX_W];

	fin = fopen(IN_FILE, "r");
	fscanf(fin, "%d\n%d\n", &G, &W);
	for (i = 0; i < G; i++)
	{
		fscanf(fin, "%d %d", EG+i, CG+i);
	}
	fclose(fin);


	/* Initializarea costurilor minime */
	for (w = 1; w <= W; w++)
		best_cost[w] = MAX_COST;
	best_cost[0] = 0;

	/* Determinam rand pe rand costurile minime pentru toate cantitatile de energie de la 0 la W */
	for (w = 1; w <= W; w++)
	{
		/* Incercand sa mai adaugam un generator la un cost deja generat */
		for (i = 0; i < G; i++)
		{
			if (w > EG[i])
			{
				if (best_cost[w] > (best_cost[w - EG[i]] + CG[i]))
				{
					best_cost[w] = best_cost[w - EG[i]] + CG[i];
				}
			}
			else
			{
				/* Dar e posibil sa obtinem un cost mai bun folosind pur si simplu un generator care are mai multa putere decat avem nevoie ! */
				if (CG[i] < best_cost[w])
				{
					best_cost[w] = CG[i];
				}
			}
		}
	}

	if (best_cost[W] == MAX_COST)
		best_cost[W] = -1;

	fout = fopen(OUT_FILE, "w");
	fprintf(fout, "%d", best_cost[W]);
	fclose(fout);

	return 0;
}