hdu 3127 WHUgirls(完全背包)

WHUgirls

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2251    Accepted Submission(s): 852


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
  
  
1 2 4 4 2 2 2 3 3 9
 

Sample Output
  
  
9

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127

题目大意:已知长宽分别为X,Y的大矩形,以及n个长和宽固定的小矩形的权值。求大矩形分割成小矩形后的能获得的最大权值。

解题思路:完全背包问题,dp[i][j]记录长宽为i,j的矩形分割后能获得的最大权值。
  递推方程为:  dp[i][j]=max(dp[i][j],dp[i-r[k].x][j]+dp[r[k].x][j-r[k].y]+r[k].v);
                     dp[i][j]=max(dp[i][j],dp[i-r[k].x][r[k].y]+dp[i][j-r[k].y]+r[k].v);      

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const maxn=1005;
int dp[maxn][maxn];
struct rec
{
    int x,y,v;
}r[15];
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=0;i<n;i++)
            scanf("%d%d%d",&r[i].x,&r[i].y,&r[i].v);
        for(i=1;i<=X;i++)
            for(j=1;j<=Y;j++)
                for(k=0;k<n;k++)
                {
                    if(r[k].x<=i&&r[k].y<=j)
                    {
                        dp[i][j]=max(dp[i][j],dp[i-r[k].x][j]+dp[r[k].x][j-r[k].y]+r[k].v);
                        dp[i][j]=max(dp[i][j],dp[i-r[k].x][r[k].y]+dp[i][j-r[k].y]+r[k].v);
                    }
                    if(r[k].y<=i&&r[k].x<=j)
                    {
                        dp[i][j]=max(dp[i][j],dp[i-r[k].y][j]+dp[r[k].y][j-r[k].x]+r[k].v);
                        dp[i][j]=max(dp[i][j],dp[i-r[k].y][r[k].x]+dp[i][j-r[k].x]+r[k].v);
                    }
                }
        printf("%d\n", dp[X][Y]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值