WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2557 Accepted Submission(s): 952
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
题解:这一题确实是道比较不错的题目,题意就是一块布,你有一台机器可以一刀切到底,现在有n种布,每种都有一个价值,你要找到一种切法使所有布的价值达到最大。
首先可以看到每种布可以取若干次,所以这题是到完全背包,只不过状态比较复杂,总共有4种状态。
1、d[x][y-y[i]]+d[x-x[i]][y[i]]+v[i];
2、d[x-x[i]][y]+d[x[i]][y-y[i]]+v[i];
3、d[x-y[i]][y]+d[y[i]][y-x[i]]+v[i];
4、d[x][y-x[i]]+d[x-y[i]][x[i]]+v[i];
其实四种状态都比较好分辨的,就是布条横竖两种状态*横切或纵切,总共4种。
又注意到每种布条可以取n次的,所以枚举布条的循环要放在最里面,保证能被其他的状态取到。
程序中是分成了两步,因为前两种的条件一样,所以放在一起写了。(之前没把d[i][j]写进去wa了几次...)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n,X,Y,x[15],y[15],v[15],T;
int d[1005][1005];
int main()
{
scanf("%d",&T);
while (T--)
{
memset(d,0,sizeof(d));
scanf("%d%d%d",&n,&X,&Y);
for (int i=1;i<=n;i++) scanf("%d%d%d",&x[i],&y[i],&v[i]);
for (int i=0;i<=X;i++)
for (int j=0;j<=Y;j++)
for (int k=1;k<=n;k++)
{
if (j-y[k]>=0 && i-x[k]>=0) d[i][j]=max(d[i][j],max(d[i][j-y[k]]+d[i-x[k]][y[k]]+v[k],d[i-x[k]][j]+d[x[k]][j-y[k]]+v[k]));
if (i-y[k]>=0 && j-x[k]>=0) d[i][j]=max(d[i][j],max(d[i-y[k]][j]+d[y[k]][j-x[k]]+v[k],d[i][j-x[k]]+d[i-y[k]][x[k]]+v[k]));
}
printf("%d\n",d[X][Y]);
}
return 0;
}