Cod sursa(job #1066507)

Utilizator dorinmoldovanMoldovan Dorin dorinmoldovan Data 24 decembrie 2013 22:22:43
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <fstream>
#include <iostream>

using namespace std;

int main(void)
{
	int G; // number of generators
	int W; // quantity of energy necessary to start the central
	int EG[1000]; // quantity of energy produced by a generator
	int CG[1000]; // necessary cost for the production of energy
	int sum; // the final cost
	int weights[10001], weights2[10001];
	int price[10001], price2[10001];

	// Read data from the input file 

	ifstream fin ("energii.in");
	fin >> G;
	fin >> W;
	for(int i = 0; i < G; i++)
	{
		fin >> EG[i];
		fin >> CG[i];
	}
	fin.close();

	for(int i = 0; i < 10001; i++) 
	{
		weights[i] = 0;
		weights2[i] = 0;
		price[i] = 0;
		price2[i] = 0;
	}

	for(int i = 0; i < G; i++)
	{
		for(int j = 0; j < 10001; j++) 
		{
			if(weights[j] != 0)
			{
				weights2[j] = weights[j];
				price2[j] = price[j];

				if(j + EG[i] < 10001) 
				{
					if(weights[j + EG[i]] == 0) 
					{
						weights2[j + EG[i]] = 1;
						price2[j + EG[i]] = price[j] + CG[i];
					}
					else
					{
						price2[j + EG[i]] = (price[j] + CG[i] > price[j + EG[i]] ? price[j + EG[i]] : price[j] + CG[i]);
					}
				}
			}
		}

		
		if(weights[EG[i]] == 0)
		{
			weights2[EG[i]] = 1;
			price2[EG[i]] = CG[i];
		}
		else 
		{
			price2[EG[i]] = CG[i] > price[EG[i]] ? price[EG[i]] : CG[i];
		}

		for(int j = 0; j < 10001; j++)
			weights[j] = weights2[j];
		for(int j = 0; j < 10001; j++)
			price[j] = price2[j];
	}

	sum = -1;

	for(int i = W; i < 10001; i++)
	{
		if(weights[i] == 1)
		{
			sum = price[i];
			break;
		}
	}

	FILE *fout;
	fout = fopen("energii.out", "w");
	fprintf(fout, "%d\n", sum);
	fclose(fout);

	return 0;
}