POJ 3624 Charm Bracelet (01背包)

#include <stdio.h>
#define MAX_CHARMS 3402
#define MAX_WEIGHT 12880

typedef struct Charm{
	int weight;
	int desirability;
}Charm;
Charm CharmArray[MAX_CHARMS + 1];
int numOfCharms;
int weightLimit;
//maxDesirability[preCharms][totalWeight],内存会超限
int maxDesirability[MAX_CHARMS + 1][MAX_WEIGHT + 1];

int main(){

	scanf("%d%d", &numOfCharms, &weightLimit);
	int charm;
	for (charm = 1; charm <= numOfCharms; charm++)
		scanf("%d%d", &CharmArray[charm].weight, &CharmArray[charm].desirability);

	int preCharms;
	int totalWeight;
	for (preCharms = 1; preCharms <= numOfCharms; preCharms++)
		for (totalWeight = 1; totalWeight <= weightLimit; totalWeight++)
			if (CharmArray[preCharms].weight > totalWeight)
				maxDesirability[preCharms][totalWeight] = maxDesirability[preCharms - 1][totalWeight];
			else {
				int tempWeight = maxDesirability[preCharms - 1][totalWeight - CharmArray[preCharms].weight] + CharmArray[preCharms].desirability;
				if (tempWeight > maxDesirability[preCharms][totalWeight])
					maxDesirability[preCharms][totalWeight] = tempWeight;
			}
		
	printf("%d\n", maxDesirability[numOfCharms][weightLimit]);

	return 0;
}


 

你可能感兴趣的:(动态规划,poj,01背包,charm,bracelet,3624)