hdu 3732(多重背包)

 

/*
     基础多重背包题
     比较有价值的就  “ 二分制压缩 ” 了,将每个个数长度num压缩成log(num)

*/

#include <cstdio>
#include <cstring>
struct One
{
	int v;
	int c;
};
One peo[100001];

int hash[15][15];
int dp[100005];

int max( int a, int b)
{
	return a > b ? a : b;
}
int main()
{
	int i,j,n,c,num;
	int temp_val,temp_col,count,sum,temp_sum;
	char s[50];
	while( scanf(" %d %d", &n ,&c) != EOF)
	{
		num=0;
		memset( hash, 0, sizeof( hash));
		memset( dp, 0, sizeof( dp));

		for ( i = 0; i < n; i++)
		{
			scanf( "%s%d%d", s, &temp_val, &temp_col);
			if(hash[temp_val][temp_col] == 0)
			{
				peo[num].v = temp_val;
				peo[num].c = temp_col;
				num++;
			}
			hash[temp_val][temp_col] ++;
		}
		for( i = 0; i < num; i++)
		{
			count=1;
			sum=0;
			temp_sum = hash[peo[i].v][peo[i].c];
			while(sum < temp_sum)
			{
				for( j = c; j - count * peo[i].c >= 0; j--)
				{
					dp[j] = max (dp[j] , dp[j - count * peo[i].c] + count * peo[i].v);
				}
				sum += count;
				if(temp_sum - sum < count * 2)
					count = temp_sum - sum;
				else
					count *= 2;
			}
		}	
		printf("%d\n", dp[c]);
	}
	return 0;
}

 

posted @ 2010-12-16 12:44  kfinder  阅读(480)  评论(0编辑  收藏  举报