您的位置:首页 > 其它

HDU 3127 WHUgirls(二维背包)

2015-02-12 23:43 375 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127

WHUgirls

Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2169 Accepted Submission(s): 825



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


额二维背包,画下图就知道了。

#include<iostream>
using namespace std;
const int maxn=1005;
int x[maxn],y[maxn],v[maxn],dp[maxn][maxn];
int t,a,b,n;
int main()
{
    cin>>t;
    while(t--)
    {
        fill(&dp[0][0],&dp[maxn][0],0);
        cin>>n>>a>>b;
        for(int i=0;i<n;i++) cin>>x[i]>>y[i]>>v[i];
        for(int i=0;i<=a;i++)
            for(int j=0;j<=b;j++)
                for(int k=0;k<n;k++)
                {
                    if(i>=x[k]&&j>=y[k])
                    {
                        dp[i][j]=max(dp[i][j],dp[i-x[k]][j]+dp[x[k]][j-y[k]]+v[k]);
                        dp[i][j]=max(dp[i][j],dp[i][j-y[k]]+dp[i-x[k]][y[k]]+v[k]);
                    }
                    if(i>=y[k]&&j>=x[k])
                    {
                        dp[i][j]=max(dp[i][j],dp[i-y[k]][j]+dp[y[k]][j-x[k]]+v[k]);
                        dp[i][j]=max(dp[i][j],dp[i][j-x[k]]+dp[i-y[k]][x[k]]+v[k]);
                    }
                }
        cout<<dp[a][b]<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: