Cod sursa(job #1511216)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 26 octombrie 2015 10:46:57
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <cstdio>

const unsigned Nmax = 17;

unsigned w[Nmax];      // w[i] = greutatea obiectului i
unsigned d[1 << Nmax]; // numarul de camioane pentru submultimea codificata

int main(void) {
    freopen("zebughil.in", "r", stdin);
    freopen("zebughil.out", "w", stdout);
    unsigned T, n, g;
    register unsigned i, j, x;

    for (T = 0; T < 3; T++) {
        scanf("%u%u", &n, &g);
        for (i = 0; i < n; i++) {
            scanf("%u", &w[i]);
        }
        for (i = 1; i < (1U << n); i++) {
            // c[i] = c[i & -i] + c[(i - 1) & i] sparge cache-ul
            x = 0;
            j = 0;
            while ((j < n) && (x <= g)) {
                if ((i >> j) & 1) {
                    x += w[j];
                }
                j++;
            }
            if (x <= g) {
                d[i] = 1;
            } else {
                d[i] = n;
                for (j = (i - 1) & i; j != 0; j = (j - 1) & i) {
                    x = d[i ^ j] + d[j];
                    if (d[i] > x) {
                        d[i] = x;
                    }
                }
            }
        }
        printf("%u\n", d[(1 << n) - 1]);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}