Cod sursa(job #446561)

Utilizator ooctavTuchila Octavian ooctav Data 26 aprilie 2010 09:01:07
Problema Zebughil Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int OO = 1000000000;
const int NMAX = 18;
const int NR_TURE = 3;
int N, G;
int plac[NMAX];
int A[1<<NMAX];
int cam[1<<NMAX];
//bitset<1<<NMAX> in_coada;
queue<int> Q;

void write()
{
	/*printf("%d %d\n", N, G);
	for(int i = 1; i <= N ; ++i)
		printf("%d ", plac[i]);
	printf("\n\n\n");*/
}
void citire()
{
	cin >> N >> G;
	for(int i = 0; i < N ; ++i)
		cin >> plac[i];
}

void init()
{
	A[0] = 1;
	fill(A + 1, A + (1 << N), OO);
	cam[0] = G;
	fill(cam + 1, cam + (1 << N), 0);
}

void BF()
{
	int x;
	Q.push(0);
	while(!Q.empty())
	{
		x = Q.front();
		Q.pop();
		for(int i = 0 ; i < N ; i++)
			if(!(1<<i & x))
			{
				int cam_nou = 0;
				int y = x + (1<<i);
				if(plac[i] > cam[x])
					cam_nou = 1;
				
				if(A[y] > A[x] + cam_nou)
				{
					A[y] = A[x] + cam_nou;
					if(!cam_nou)
						cam[y] = cam[x] - plac[i];
					else
						cam[y] = G - plac[i];
					Q.push(y);
					
				}
				if(A[y] == A[x] + cam_nou && cam[y] > cam[x] + plac[i])
				{
					cam[y] = cam[x] + plac[i];
					Q.push(y);
				}
			}
	}
}

int main()
{
	freopen("zebughil.in","r",stdin);
	freopen("zebughil.out","w",stdout);
	for(int i = 1 ; i <= NR_TURE ; i++)
	{
		citire();
		init();
		BF();
		write();
		printf("%d\n",A[(1<<N) - 1]);
	}
	return 0;
}