背包问题-HDU-3127 WHUgirls

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.

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

 

分析:这道题的主要意思是有一块布,有一台机器可以对其进行切割,但只能一刀切到底。有n种不同形状规格的布,价值也各有不同,要求尽可能使得剪切过的布条加起来的总价值最大,当然,并没有说明一定要把布料用完。这很明显是背包问题,而且是完全背包,情况可以这么判断:这块布可以横放,也可以纵放。也可以横切,也可以纵切,这样就有四种情况,如下图所示:

图一和图二是横放的两种情况,图三和图四是纵放的两种情况。

1.f[i][j-y[k]]+f[i-x[k]][y[k]]+c[i];

2.f[x[k]][j-y[k]]+f[i-x[k]][j]+c[i];

3.f[i-y[k]][j]+f[j-x[k]][y[k]]+c[i];

4.f[x[k]][i-y[k]]+f[i][j-y[k]]+c[i];

附上AC源代码:

 

#include<stdio.h>
#include<string.h>
int T,X,Y;
int f[1005][1005];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int i,j,k,N;
	int x[15],y[15],c[15];
	scanf("%d",&T);
    while(T--)
		{
			scanf("%d %d %d",&N,&X,&Y);
			for(i=0;i<N;i++)
				scanf("%d %d %d",&x[i],&y[i],&c[i]);
			for(i=0;i<=X;i++)
				for(j=0;j<=Y;j++)
					for(k=0;k<N;k++)
					{
						if(i>=x[k]&&j>=y[k])
							f[i][j]=max(f[i][j],max(f[i-x[k]][j]+f[x[k]][j-y[k]]+c[k],f[i][j-y[k]]+f[i-x[k]][y[k]]+c[k]));
						if(i>=y[k]&&j>=x[k])
							f[i][j]=max(f[i][j],max(f[i-y[k]][j]+f[y[k]][j-x[k]]+c[k],f[i][j-x[k]]+f[i-y[k]][x[k]]+c[k])); 
					}
			printf("%d\n",f[X][Y]);
		}	
	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值