WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2323 Accepted Submission(s): 884
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
动态规划就是倒着看。可以看成是矩形拼接。dp[i][j]就是剪出长I宽J的矩形所需要的最大价值。这块矩形要进行裁剪就是,将矩形分成了三个小矩形,两个是由裁剪剩余的,另外一个就是目标矩形,所以这块矩形的DP值就是目标矩形的价值加上两块裁剪剩余的矩形价值。同时应为目标矩形可以横摆竖摆,对应的也有两种裁剪剩余矩形情况。所以DP如下。做题的时候DP方程没想到。最后写出了DFS的代码。总体思路居然都是一样的。可惜了DP不熟练。
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long numx[1010],numy[1010],numc[1010];
long long dp[1010][1010];
int main()
{
long long T;
cin >> T;
while(T--)
{
long long n,x,y;
cin >> n >> x >> y;
for(int i=0;i<n;i++)
cin >> numx[i] >> numy[i] >> numc[i];
long long ans = 0;
memset(dp,0,sizeof(dp));
for(int i=0;i<=x;i++)
for(int j = 0;j<=y;j++)
for(int k=0;k<n;k++)
{
if(i>=numx[k]&&j>=numy[k])
{
dp[i][j] = max(dp[i][j],
max(dp[i-numx[k]][j]+dp[numx[k]][j-numy[k]],
dp[i-numx[k]][numy[k]]+dp[i][j-numy[k]])+numc[k]);
}
if(i>=numy[k]&&j>=numx[k])
{
dp[i][j] = max(dp[i][j],
max(dp[i-numy[k]][j]+dp[numy[k]][j-numx[k]],
dp[i-numy[k]][numx[k]]+dp[i][j-numx[k]])+numc[k]);
}
ans = max(ans,dp[i][j]);
}
cout<<ans<<endl;
}
return 0;
}