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\n",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\n",dp[X][Y]);
}
return 0;
}

HDU 3127 WHUgirls dp背包问题的更多相关文章

  1. HDU 3127 WHUgirls(完全背包)

    HDU 3127 WHUgirls(完全背包) http://acm.hdu.edu.cn/showproblem.php? pid=3127 题意: 如今有一块X*Y的矩形布条, 然后有n种规格的x ...

  2. HDU 3127 WHUgirls(DP 完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127 题目大意:将一块长x宽y的矩形布料,剪成小的矩形(每个给定的小矩形都对应一个价值),使得所有小矩 ...

  3. HDU 3127 WHUgirls【二维完全背包】

    题意:给出一个长为a,宽为b的布,再给出n个围巾的规格(长x,宽y,价值c),问怎样裁剪能够得到最大的价值. ----第一次做的时候不会---然后放到今天做--发现还是不会---于是又--看题解了-- ...

  4. HDU 1561 树形DP背包问题

    这是自己第一道背包上树形结构问题,不是很理解这个概念的可以先看看背包九讲 自己第一次做,看了一下别人的思路,结合着对简单背包问题的求解方式自己一次AC了还是有点小激动的 题目大意是: 攻克m个城市,每 ...

  5. HDU 3127 WHUgirls

    二维完全背包,理解似乎还不够全面,过几天回来再看看这题. #include<cstdio> #include<cstring> #include<cmath> #i ...

  6. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  7. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  8. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  9. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. css3实现色子自动翻转效果

    原文:css3实现色子自动翻转效果 css3使我们能够跳出2d空间,实现3维空间的动画效果,这里给出一个自动翻转的3d色子动画效果制作过程. 第一步,首先进行HTML的布局,对于3D效果,布局有一定的 ...

  2. Enum:枚举

    原文:Enum:枚举 枚举 (enum) 是值类型的一种特殊形式,它从 System.Enum 继承而来,并为基础的基元类型的值提供替代名称.枚举类型有名称.基础类型和一组字段.基础类型必须是一个内置 ...

  3. 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例(原创)

    天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文“寻求合作伙伴编写<深入理解 MonkeyRunner>书籍“.但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在 ...

  4. win7访问xp共享访问不了

    这个问题不是一天两天了,经历几次了,所以记下来. 1. 一些XP对用户权限作了特殊的优化设置.限制了只有guest用户才能用于做局域网共享用户. 2. 大多数时候,需要设置一个密码,才能用于访问. 3 ...

  5. 验证(C#和正则表达式)

    原文:验证(C#和正则表达式) 我们经常会需要验证字符串的格式,比如密码长度范围.电子邮件格式.固定电话号码和手机号码格式等,这个时候我们经常会需要用到正则表达式.但是正则表达式用起来性能会低一点,所 ...

  6. 11g R2RAC Dynamic remastering

    In this post, I will demonstrate dynamic remastering of the resources in RAC . In RAC, every data bl ...

  7. 设计与实现的简单和经常使用的权限系统(五岁以下儿童):不维护节点的深度level,手工计算level,树形结构

     以这种方式.和第三的类似介绍.所不同的是.深度未在数据库中存储节点level,添加和更改时间,护.而是,在程序中,实时去计算的. 至于后面的,依照level升序排序,再迭代全部的节点构造树,与第三篇 ...

  8. 【SSH三个框架】Hibernate第八部分基础:经营-many关系

    在Hibernate在-many关系.它通常不使用.由于当数据库查询复杂度太高时. 我们在这里做的是学生和教师,学生可以有多个老师,教师可以有多个学生. watermark/2/text/aHR0cD ...

  9. Unity3D开发必备神器(Visual Studio Tools for Unity)

    Unity3D开发必备神器(Visual Studio Tools for Unity) 开发Unity3D程序你用的什么IDE呢? 1.MonoDevelop 2.VS 可能你的回答是这样的,我用的 ...

  10. 通过如何通过js实现复制粘贴功能

    在ie中window.clipboardData(剪切板对象)是可以被获取,所以利用这个方法我们可以实现在IE当中复制粘贴的功能,demo如下! <html> <head> & ...