首页 > ACM题库 > HDU-杭电 > HDU 3003-Pupu-背包问题-[解题报告]HOJ
2014
02-27

HDU 3003-Pupu-背包问题-[解题报告]HOJ

Pupu

问题描述 :

There is an island called PiLiPaLa.In the island there is a wild animal living in it, and you can call them PuPu. PuPu is a kind of special animal, infant PuPus play under the sunshine, and adult PuPus hunt near the seaside. They fell happy every day.

But there is a question, when does an infant PuPu become an adult PuPu?
Aha, we already said, PuPu is a special animal. There are several skins wraping PuPu’s body, and PuPu’s skins are special also, they have two states, clarity and opacity. The opacity skin will become clarity skin if it absorbs sunlight a whole day, and sunshine can pass through the clarity skin and shine the inside skin; The clarity skin will become opacity, if it absorbs sunlight a whole day, and opacity skin will keep sunshine out.

when an infant PuPu was born, all of its skins were opacity, and since the day that all of a PuPu’s skins has been changed from opacity to clarity, PuPu is an adult PuPu.

For example, a PuPu who has only 3 skins will become an adult PuPu after it born 5 days(What a pity! The little guy will sustain the pressure from life only 5 days old)

Now give you the number of skins belongs to a new-laid PuPu, tell me how many days later it will become an adult PuPu?

输入:

There are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0

输出:

There are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0

样例输入:

2
3
0

样例输出:

1
2

背包

#include "stdio.h"
 #include "string.h"
 #include "stdlib.h"
 
 typedef struct _Node{
 	int price, value;
 	struct _Node *next;
 }Node, *pNode;
 
 pNode brand[11];
 int dp[11][10001];
 
 int n, m, k;
 
 int max(int a, int b){
 	return a>b?a:b;
 }
 
 int main(){
 	int i, j, b, p, v;
 	pNode t;
 	freopen("in.txt", "r", stdin);
 	while(scanf("%d %d %d", &n, &m, &k)!=EOF){
 		memset(brand, 0, sizeof(brand));
 		for(i=0; i<n; i++){
 			scanf("%d %d %d", &b, &p, &v);
 			t = (pNode)malloc(sizeof(Node));
 			t->price = p;
 			t->value = v;
 			t->next = brand[b];
 			brand[b] = t;
 		}
 		memset(dp, 0, sizeof(dp));
 		for(i=1; i<=k; i++){
 			t = brand[i];
 			while(t){
 				for(j=10000; j>=0; j--){  //注意这里,j是从大往小走,是为了避免同一商品多次放入
 					if(j>=t->price)
 						dp[i][j] = max(dp[i][j], max(dp[i-1][j-t->price], dp[i][j-t->price]) +t->value);
 					//else
 					//	dp[i][j] = max(dp[i][j], dp[i-1][j]);
 				}
 				t = t->next;
 			}
 		}
 		if(dp[k][10000])
 			printf("%d\n", dp[k][10000]);
 		else
 			printf("Impossible\n");
 	}
 	return 0;
 }

参考:http://blog.csdn.net/chaoojie/article/details/8251325