Cod sursa(job #287396)

Utilizator ScrazyRobert Szasz Scrazy Data 24 martie 2009 20:25:01
Problema Zebughil Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f

const int maxn = 18;

bool dp[1<<maxn][maxn];
ll cost[maxn];
ll G;
int n;
int ares = inf;

#define isSet(mask, i) ((mask&(1<<(i))))
#define Set(mask, i) ((mask|(1<<(i))))

void read()
{
    scanf("%d%lld\n", &n, &G); 
    for (int i = 0; i < n; ++i) scanf("%lld", &cost[i]);
}

bool solve(ll mask, ll cap, int num)
{ 
    bool &res = dp[mask][num];
    if (mask == (1<<n)-1)
    {
	if (num < ares) ares = num;
	return 1;
    }

    if (res) return res;

    for (int i = 0; i < n; ++i)
    {
	if (isSet(mask, i)) continue;
	
	if (cost[i] > cap)
	    solve(Set(mask,i), G - cost[i], num + 1);
	else
	    solve(Set(mask,i), cap - cost[i], num);
    }
}

int main()
{
    freopen("zebughil.in","r",stdin);
    freopen("zebughil.out","w",stdout); 

    for (int test = 0; test < 3; ++test)
    {
	read();
	memset(dp, false, sizeof(dp));
	//if (check()) printf(;
	solve(0, G, 1);
	printf("%d\n", ares);
    }

    return 0;
}