Backpack nine talk about the multi-knapsack problem inside. Each block has a height and quantity, and there is a maximum height to the position limit. We first have to sort the height limit before DP, because from the intuitive point of view, we also need to deal with the smaller blocks that can reach the height, so that we can get the maximum value. For example the first height is 11, the limit height is 100, the second limit height is 10, that if processed directly, the second one will not be processed to.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX (x, y) (x>y?x:y) struct block{ int h;int a;int c;}; struct block Data[410];int dp[40010]={0};int cmp (const void*p1,const VOID*P2) {return (* (block*) p1). a > (* (Block *) p2). A?1:-1;} int main () {int k,i,j,m;scanf ("%d", &k); for (i=0;i<k;i++) {scanf ("%d%d%d",&data[i].h,&data[i].a,& DATA[I].C);} Qsort (Data,k,sizeof (data[0]), CMP), memset (Dp,0,sizeof (DP)); for (i=0;i<k;i++) {for (m=1;m<=data[i].c;m++) for ( j=data[i].a;j>=data[i].h;j--) Dp[j]=max (dp[j],dp[j-data[i].h]+data[i].h);} int ans=0;for (i=0;i<=data[k-1].a;i++) Ans=max (Ans,dp[i]);p rintf ("%d\n", ans); return 0;}
POJ Dynamic planning DP-2392 Space Elevator