All pastes #2092432 Raw Edit

Someone

public text v1 · immutable
#2092432 ·published 2011-10-22 06:17 UTC
rendered paste body
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct item {
	int c, f, p, o, nbUses;
	string name;
};

int bestWeight = INT_MAX;
vector<int> bestChoice;
vector<item> items;

int W(struct item i)
{
	return i.c + i.f + i.o + i.p;
}

bool addingIsUsefull(int index, int C, int F, int P)
{
	return  (items[index].c > 0 && C > 0) ||
			(items[index].f > 0 && F > 0) ||
			(items[index].p > 0 && P > 0);
}

void solve(vector<int>& currentChoices, int currentWeight, int C, int F, int P, int index)
{
	if (C <= 0 && F <=0 && P <= 0)
	{
		if(currentWeight < bestWeight)
		{
			bestWeight = currentWeight;
			bestChoice.clear();
			vector<int>::iterator start = currentChoices.begin();
			vector<int>::iterator end = currentChoices.end();
			vector<int>::iterator it = bestChoice.begin();

			bestChoice.insert(it, start, end);
		}
		return;
	}

	if (index == items.size())
	{
		return;
	}
	
	if(addingIsUsefull(index,C,F,P))
	{
		currentWeight += W(items[index]);
		currentChoices.push_back(index);
		solve(currentChoices,currentWeight, C-items[index].c,
											F-items[index].f,
											P-items[index].p, index);
		currentChoices.pop_back();
	}

	solve(currentChoices,currentWeight,C,F,P,index+1);
}

void LoadItems(vector<item>& itemsToLoad, int& maxC, int& maxF, int& maxP)
{
	cin >> maxC;
	cin >> maxF;
	cin >> maxP;

	int k;
	cin >> k;

	for (int i = 0; i < k; i++)
	{
		item Item;
		cin >> Item.name;
		cin >> Item.c;
		cin >> Item.f;
		cin >> Item.p;
		cin >> Item.o;
		Item.nbUses = 0;

		itemsToLoad.push_back(Item);
	}
}

int main()
{
	int C, F, P;
	vector<int> choices;
	
	LoadItems(items, C, F, P);

	if(items.size() == 0)
	{
		cout << "Null" << endl;
		return 0;
	}

	solve(choices, 0, C, F, P, 0);
	
	if(bestWeight == INT_MAX)
	{
		cout << "Null" << endl;
		return 0;
	}

	for(unsigned int i = 0; i < bestChoice.size(); ++i)
	{
		items[bestChoice[i]].nbUses++;
	}

	for(unsigned int i = 0; i < items.size(); ++i)
	{
		cout << items[i].name << " " << items[i].nbUses << endl;
	}

	return 0;
}