HDU3127:WHUgirls(完全背包)

来源:互联网 发布:如何优化网站页面 编辑:程序博客网 时间:2024/06/05 17:04
Problem Description
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.
 

Input
The 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.
 

Output
Output 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


 

题意:给出布条有n种剪法还有布条的长宽分别为X,Y,然后给出每种剪法的小布条的长宽与价值,要求最后剪除布条的总最大价值

思路:这道题布条的总大小即为容量,而且每种剪法的小布条都有价格,很明显的背包题,而且由于每种减法可以多次使用,这就构成了一个完全背包,由于布条有长宽,所以使用二维数组。

要注意的是小布条的长宽并不是固定的,可以旋转,所以剪法有两种,得出两个状态方程

方程1:dp[i][j] = max(dp[i][j],max((dp[i-a[k].x][j]+dp[a[k].x][j-a[k].y]),(dp[i][j-a[k].y]+dp[i-a[k].x][a[k].y]))+a[k].v);

方程2:dp[i][j] = max(dp[i][j],max((dp[i-a[k].y][j]+dp[a[k].y][j-a[k].x]),(dp[i][j-a[k].x]+dp[i-a[k].y][a[k].x]))+a[k].v);

以方程1为例

要注意的是,大布条剪去小布条后剩下的布不一定是矩形,这种情况下剩下的布条可以分成两个矩形,所以总价值就是这三个矩形之和,而剩下的矩形又可以看做心的大矩形,由此循环得到

max((dp[i-a[k].x][j]+dp[a[k].x][j-a[k].y]),(dp[i][j-a[k].y]+dp[i-a[k].x][a[k].y]]))+a[k].v

 

#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;struct node{    int x,y,v;}a[20];int dp[1005][1005];int main(){    int i,j,k,n,X,Y,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&X,&Y);        for(i = 0;i<n;i++)        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].v);        memset(dp,0,sizeof(dp));        for(i = 0;i<=X;i++)        {            for(j = 0;j<=Y;j++)            {                for(k = 0;k<n;k++)                {                    if(i>=a[k].x && j>=a[k].y)                    dp[i][j] = max(dp[i][j],max((dp[i-a[k].x][j]+dp[a[k].x][j-a[k].y]),(dp[i][j-a[k].y]+dp[i-a[k].x][a[k].y]))+a[k].v);                    if(i>=a[k].y && j>=a[k].x)                    dp[i][j] = max(dp[i][j],max((dp[i-a[k].y][j]+dp[a[k].y][j-a[k].x]),(dp[i][j-a[k].x]+dp[i-a[k].y][a[k].x]))+a[k].v);                }            }        }        printf("%d\n",dp[X][Y]);    }    return 0;}


 

原创粉丝点击