Cod sursa(job #287356)

Utilizator ScrazyRobert Szasz Scrazy Data 24 martie 2009 19:42:35
Problema Zebughil Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f

const int maxn = 18;

int dp[1<<maxn];
ll cost[maxn];
ll G;
int n;

#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]);
}

void solve(ll mask, ll cap, int kamion)
{ 
    if (mask == (1<<n)-1)
    { 
	dp[mask] = min(dp[mask], kamion); 
	return ;
    }

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

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

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

    return 0;
}