Pagini recente » Cod sursa (job #2747347) | Cod sursa (job #3135550) | Cod sursa (job #1434432) | Cod sursa (job #2719094) | Cod sursa (job #388703)
Cod sursa(job #388703)
#include <stdio.h>
#include <queue>
#define NMAX 1<<17
#define LMAX 18
#define INF 25
using namespace std;
int t=3,n,g,C[LMAX],nr;
struct coada
{
int a,b;
};
coada best[NMAX];
queue <int> Q;
void read()
{
scanf("%d%d",&n,&g);
int i; nr=0;
for (i=1; i<=n; i++)
{
scanf("%d",&C[i]);
nr+=1<<(i-1);
}
}
void init()
{
int i;
for (i=1; i<(1<<n); i++)
best[i].a=INF;
}
void solve()
{
Q.push(0);
int i,x,y;
while (!Q.empty())
{
x=Q.front();
Q.pop();
for (i=1; i<=n; i++)
if (!(x & 1<<(i-1)))
{
y=x ^ (1<<(i-1));
if (best[x].b-C[i]>=0)
if (best[x].a<best[y].a)
{
best[y].a=best[x].a,best[y].b=best[x].b-C[i];
Q.push(y);
}
if (best[x].b-C[i]<0)
if (best[x].a+1<best[y].a)
{
best[y].a=best[x].a+1,best[y].b=g-C[i];
Q.push(y);
}
}
}
}
int main()
{
freopen("zebughil.in","r",stdin);
freopen("zebughil.out","w",stdout);
while (t)
{
read();
init();
solve();
printf("%d\n",best[nr].a);
t--;
}
return 0;
}