HDU 3127 whugirls (full backpack)
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3127
Question:
There is now an X * y rectangular cloth, and then there are N types of X [I] * Y [I] Small cloth, each of which can sell the value of Val [I. how much is the maximum value of your original x * y cloth? Each cut can only be horizontal or vertical, and a knife is used to the end.
Analysis:
This question seems troublesome, but it is very easy to understand the principle. considering the remaining rectangular strips as capacity, we need to find the sum of the most valuable strips in the limited capacity.
So that DP [I] [J] = X indicates that the maximum value of the current length I and width J can be cut is X.
Note the following conclusion:: Each time we cut a small rectangle from a large rectangle, we always cut along the top corner of the large rectangle without losing the optimal solution.
For example, if we want to cut a small x y rectangle from an I * j rectangle, there are four ways (the large rectangle can choose the vertical knife or the horizontal knife first, the small rectangle can be rotated ):
(View the figure above carefully .)
With the above figure, our recursive formula is shown below:
If (I> = R [K]. X & J> = R [K]. Y)
DP [I] [J] = max (DP [I] [J], max (DP [I-r [K]. x] [J] + dp [R [K]. x] [J-R [K]. y], DP [I-r [K]. x] [R [K]. y] + dp [I] [J-R [K]. y]) + R [K]. val );
If (I> = R [K]. Y & J> = R [K]. X)
DP [I] [J] = max (DP [I] [J], max (DP [R [K]. y] [J-R [K]. x] + dp [I-r [K]. y] [J], DP [I-r [K]. y] [R [K]. x] + dp [I] [J-R [K]. x]) + R [K]. val );
The above two formulas seem to be very complex. In fact, it is very easy to understand the above figure. The essence is:
Maximum value that can be obtained from the original rectangle = max (the value of the small rectangle + the maximum value that can be obtained from the remaining two rectangles after a horizontal and vertical one-size-fits-all cut)
That is, to find the value and value of the three rectangles after cutting, to see which method to cut the remaining rectangle value and the maximum (after the two knives, the original rectangle will inevitably become three new rectangles)
Initialization: DP is all 0.
Final requirement: DP [x] [Y].
Note:The cyclic order of dimension J and item id I under the constraints of a complete backpack problem can be exchanged. HoweverThis question must first cycle the dimensions of X and Y, and then the dimensions of item numbers.Because the optimal solution to the general full knapsack problem does not require the order of item selection, you can first partition any item. but for the original rectangle, cutting the small rectangle at the top corner of it is obviously different. It is possible that you will not get the optimal solution if you cut the first rectangle, however, you must first cut the rectangle 3 to get the optimal solution (think about it carefully ).
AC code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1000+5;int X,Y;//原始矩形宽和高int n; //有多少个小布条struct Node//每个小布条{ int x,y; int val;}r[10+5];int dp[maxn][maxn];int main(){ int T; scanf("%d",&T); while(T--) { //读取输入+初始化 scanf("%d%d%d",&n,&X,&Y); for(int i=1;i<=n;i++) scanf("%d%d%d",&r[i].x,&r[i].y,&r[i].val); memset(dp,0,sizeof(dp)); //递推,注意:先X和Y,然后才是矩形编号. //如果先循环矩形编号,就错了. for(int i=0;i<=X;i++) for(int j=0;j<=Y;j++) for(int k=1;k<=n;k++) { if(i>=r[k].x && j>=r[k].y) dp[i][j]=max( dp[i][j] , max( dp[i-r[k].x][j]+dp[r[k].x][j-r[k].y] , dp[i-r[k].x][r[k].y]+dp[i][j-r[k].y] )+r[k].val ); if(i>=r[k].y && j>=r[k].x) dp[i][j]=max( dp[i][j] , max( dp[r[k].y][j-r[k].x]+dp[i-r[k].y][j] , dp[i-r[k].y][r[k].x]+dp[i][j-r[k].x] )+r[k].val ); } //输出结果 printf("%d\n",dp[X][Y]); }}
HDU 3127 whugirls (full backpack)