HDU 3127 WHUgirls dp背包问题


WHUgirls

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


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, Yindicating 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
 

Source
 

Recommend
lcy
 


题解:

这是一道模仿现实的题目。它给的是长跟宽,你要知道。。现实中长跟宽是可以互换的。当然,你也可以这样再优化一下,可能存在长跟宽相同,价值不同的,保留最大的就好了。


注意。。题目中的切开是一切到底。不是任意切!!这样列出的DP方程是不一样的
刚开始我的DP方程是
i,j表示长为i,宽为j的空间
f[i][j]=max(f[i][j],f[i][j-k]+f[i][k],f[i-k][j]+f[k][j]) + 一点点优化。。 f[i][j]初始状态就是所给的一系列长跟宽跟价值. 
f[p[k].x][p[k].y]=p[k].c
。。不过这样水不过去。


后来改写成2维的背包。593MS过掉。


dp[i][j]=max(dp[i-p[k].x][j]+p[k].c+dp[p[k].x][j-p[k].y],dp[i][j],dp[i-p[k].x][p[k].y]+dp[i][j-p[k].y]+p[k].c);


要推出这条式子,你拿出一块草稿画一下就好了。  先中间切下去,然后在右边那小块再横着来一下。就是了。。


/*
 * @author ipqhjjybj
 * @date  20130724
 *
 */

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 1005
#define clr(x,k) memset((x),(k),sizeof(x))
#define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

typedef vector<int> vi;
typedef stack<int> si;
typedef vector<string> vs;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i = 0;i < n;++i)
#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)

#define min(a,b) ((a)<(b)?(a):(b))
struct node{
    int x,y,c;
}p[MAXN];
int dp[MAXN][MAXN];
int X,Y,n;
int max(int a,int b){
    return a>b?a:b;
}
int main(){
    //freopen("3127.in","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        clr(dp,0);
        scanf("%d %d %d",&n,&X,&Y);
        for(int i=0,a,b,c;i<n;i++){
            scanf("%d %d %d",&a,&b,&c);
            p[i<<1].x=a,p[i<<1].y=b,p[i<<1].c=c;
            p[i<<1|1].x=b,p[i<<1|1].y=a,p[i<<1|1].c=c;
        }
        n <<=1;
        for(int i=0;i<=X;i++)
            for(int j=0;j<=Y;j++){
                for(int k=0;k<n;k++){
                    if(i>=p[k].x&&j>=p[k].y){
                        dp[i][j]=max(max(dp[i-p[k].x][j]+p[k].c+dp[p[k].x][j-p[k].y],dp[i][j]),
                                         dp[i-p[k].x][p[k].y]+dp[i][j-p[k].y]+p[k].c);
                        //printf("dp[%d][%d]=%d
",i,j,dp[i][j]);
                    }
                }
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                dp[i][j]=max(dp[i][j],dp[i][j-1]);
            }
        printf("%d
",dp[X][Y]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/jiangu66/p/3214923.html