hdoj 1059 Dividing【DP-多重背包】

/*
145153 	2012-02-01 19:49:03 	Accepted 							3.3.1 	218MS 	220K 	1270B 	G++ 	ylwh
145151 	2012-02-01 19:48:06 	Accepted 							3.3.1 	187MS 	220K 	1242B 	G++ 	ylwh
145150 	2012-02-01 19:47:03 	Presentation Error 					3.3.1 	187MS 	220K 	1239B 	G++ 	ylwh
145148 	2012-02-01 19:46:32 	Runtime Error(ACCESS_VIOLATION) 	3.3.1 	0MS 	180K 	1239B 	G++ 	ylwh
*/
#include <stdio.h>
#include <string.h>
#define MAX 80000
bool f[MAX];
int v_max;
void ZeroOnePack(int cost)
{
	for(int v = v_max; v >= 0; v--)
		if(f[v-cost] == true)
			f[v] = true;
}
void CompletePack(int cost)
{
	for(int v = cost; v <= v_max; v++)
		if(f[v-cost] == true)
			f[v] = true;
}
void MultiplePack(int cost, int amount)
{
	if(cost * amount >= v_max){
		CompletePack(cost);
		return ;
	}
	int k = 1;
	while(k < amount){
		ZeroOnePack(k * cost);
		amount -= k;
		k *= 2;
	}
	ZeroOnePack(amount * cost);
}
int main()
{
    int cases = 0, num[7];
    while(true)
    {
        v_max = 0;
        memset(f, false, sizeof(f));
        for(int i=1; i<=6; i++)
        {
            scanf("%d", num + i);
            v_max += num[i] * i;
        }
        if( !v_max )    break;
        printf("Collection #%d:\n", ++cases);
        if( v_max&1 )	goto X;
        v_max /= 2;
        f[0] = true;
        for(int i=1; i<=6; i++)
            MultiplePack(i, num[i]);
X:
        if(f[v_max] == false)
            printf("Can't be divided.\n");
		else
            printf("Can be divided.\n");
        printf("\n");
	}
	return 0;
}

参考背包九讲...看完就会了

你可能感兴趣的:(hdoj 1059 Dividing【DP-多重背包】)