【背包专题】K

来源:互联网 发布:淘宝金利来男士钱包 编辑:程序博客网 时间:2024/05/01 22:16
There are many pretty girls in Wuhan University, and as we know, every girl loves pretty clothes, so do they. One day some of them got a huge rectangular cloth and they want to cut it into small rectangular pieces to make scarves. But different girls like different style, and they voted each style a price wrote down on a list. They have a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically, and ask you to use this machine to cut the original huge cloth into pieces appeared in the list. Girls wish to get the highest profit from the small pieces after cutting, so you need to find out a best cutting strategy. You are free to make as many scarves of a given style as you wish, or none if desired. Of course, the girls do not require you to use all the cloth.

InputThe first line of input consists of an integer T, indicating the number of test cases. 
The first line of each case consists of three integers N, X, Y, N indicating there are N kinds of rectangular that you can cut in and made to scarves; X, Y indicating the dimension of the original cloth. The next N lines, each line consists of two integers, xi, yi, ci, indicating the dimension and the price of the ith rectangular piece cloth you can cut in. 
OutputOutput the maximum sum of prices that you can get on a single line for each case. 

Constrains 
0 < T <= 20 
0 <= N <= 10; 0 < X, Y <= 1000 
0 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000 
Sample Input

12 4 42 2 23 3 9

Sample Output

9题意:给定一个x*y的矩形,输入n块xi*yi的小矩形,价值为value[i],问分割矩形为小矩形后能够得到的最大价值。思路:这道题给定矩形是无序的,我们并不确定哪个小矩形作为第一个从大矩形中分割出去能够得到最大价值,所以把小矩形长宽作为外循环,矩形序号作为内循环。在比较最优值时我们加上判断条件,因为总共分为两类情况,两类情况里又分为两种分割方式。假设小矩形长为x[i],宽为y[i],价值为value[i]。第一种情况:矩形长和宽大于等于小矩形长和宽即 x>=x[i]&&y>=y[i]。第一种分割方式:dp[x[i]][k-y[i]]+dp[j-x[i]][k]+value[i]
第二种分割方式:dp[j-x[i]][y[i]]+dp[j][k-y[i]]+value[i]第二种情况:矩形的长和宽大于等于小矩形的宽和长即 x>=y[i]&&y>=x[i]
第一种分割方式:dp[j][k-x[i]]+dp[j-y[i]][x[i]]+value[i]第二种分割方式:dp[j-y[i]][k]+dp[y[i]][k-x[i]]+value[i]
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1010int dp[N][N],x[20],y[20],value[20];int main(){    int t,n,X,Y,i,j,k;    scanf("%d",&t);    while( t--)    {        scanf("%d%d%d",&n,&X,&Y);        memset(dp,0,sizeof(dp));        for(i = 1; i <= n; i ++)            scanf("%d%d%d",&x[i],&y[i],&value[i]);        for(j = 0; j <= X; j ++)            for(k = 0; k <= Y; k ++)                for(i = 1; i <= n; i ++)                {                    if(j >= x[i]&&k >= y[i])//第一种情况                        dp[j][k] = max(dp[j][k],max(dp[x[i]][k-y[i]]+dp[j-x[i]][k],dp[j-x[i]][y[i]]+dp[j][k-y[i]])+value[i]);                    if(j >= y[i]&&k >= x[i])//第二种情况                        dp[j][k] = max(dp[j][k],max(dp[j][k-x[i]]+dp[j-y[i]][x[i]],dp[j-y[i]][k]+dp[y[i]][k-x[i]])+value[i]);                }        printf("%d\n",dp[X][Y]);    }    return 0;}

 

原创粉丝点击