hdu-3127 题目点我
WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2260 Accepted Submission(s): 856
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.
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
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的背包所能获得的最大权值。
那么对于任意的i,j,有dp[i][j]=max(dp[i-x][j]+dp[x][j-y], dp[i][j-y]+dp[i-x][y])+c,x,y,c代表物品的长宽和权值。
这是对两种裁剪求最大,可以画图理解。
而每个物品又有两种摆放方式(横放和竖放),因此有两个转移方程。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=1007;
int n, X, Y;
int dp[maxn][maxn];
struct rectangel //物品的结构体
{
int a, b, c;
}rect[15];
void init()
{
memset(dp, 0, sizeof(dp));
}
int cut(int x, int y, int a, int b) //判断横放或竖放背包能否容下物品
{
if(x>=a&&y>=b) return 1;
return 0;
}
void solve()
{
for(int x=1;x<=X;x++){
for(int y=1;y<=Y;y++){
for(int i=0;i<n;i++){
int t1=0, t2=0;
if(cut(x, y, rect[i].a, rect[i].b)) //横放
t1=max(dp[x-rect[i].a][y]+dp[rect[i].a][y-rect[i].b], dp[x][y-rect[i].b]+dp[x-rect[i].a][rect[i].b])+rect[i].c; //两种裁剪取最大
if(cut(x, y, rect[i].b, rect[i].a)) //竖放
t2=max(dp[x-rect[i].b][y]+dp[rect[i].b][y-rect[i].a], dp[x][y-rect[i].a]+dp[x-rect[i].b][rect[i].a])+rect[i].c;
int t=max(t1, t2);
dp[x][y]=max(dp[x][y], t);
}
}
}
printf("%d\n", dp[X][Y]);
}
int main()
{
int T;
scanf("%d", &T);
while(T--){
init();
scanf("%d%d%d",&n, &X, &Y);
for(int i=0;i<n;i++){
scanf("%d%d%d", &rect[i].a, &rect[i].b, &rect[i].c);
}
solve();
}
return 0;
}