二维dp
来源:互联网 发布:千牛 淘宝助理 编辑:程序博客网 时间:2024/06/05 16:40
原题http://acm.hdu.edu.cn/showproblem.php?pid=3127
WHUgirls
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2050 Accepted Submission(s): 780
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
12 4 42 2 23 3 9
Sample Output
9
//首先应该能想到这是个完全背包的问题,但是要考虑到边长。所以开个二维数组,dp[i][j],代表前i长,前j宽的时候最大的价值是多少//因为在进行分割的时候只能是横这切或者竖着切,并且小的长方形既可以横这摆,也可以竖着摆。所以有四种状态。#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#include <string>#include <math.h>#include <limits.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <deque>#include <vector>#include <set>#include <map>using namespace std;#define N 1000 + 10#define M 10 + 2struct node{ int x; int y; int val;}a[N];int dp[N][N];int max(int a,int b){ return a>b?a:b;}int main(){ int T,n; int X,Y; while(~scanf("%d",&T)){ while(T--){ memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); scanf("%d%d%d",&n,&X,&Y); int i; for(i=0;i<n;i++){ scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].val); } int j,k; for(i=1;i<=X;i++){ for(j=1;j<=Y;j++){ for(k=0;k<n;k++){ if(i>=a[k].x && j>=a[k].y) dp[i][j] = max(dp[i][j],max(dp[i-a[k].x][j]+dp[a[k].x][j-a[k].y],dp[i][j-a[k].y]+dp[i-a[k].x][a[k].y])+a[k].val);//因为i,j是从小到大的。所以每次取的时候都能去最优解。因为每次分割的时候都能变成三个小的矩形。所以要取最优 if(i>=a[k].y && j>=a[k].x) dp[i][j] = max(dp[i][j],max(dp[i-a[k].y][j]+dp[a[k].y][j-a[k].x],dp[i][j-a[k].x]+dp[i-a[k].y][a[k].x])+a[k].val); } } } printf("%d\n",dp[X][Y]); } } return 0;}
1 1
- 二维dp
- dp day3/4-二维dp
- HDOJ FATE 二维DP
- hdu2854 Beans 二维dp
- 二维DP 建房子
- poj 3257 二维dp
- poj-3661-二维dp
- SGU104 二维dp
- 【二维dp】最长等差数列
- hdoj2159FATE【二维dp】
- hdu4504基础dp,二维
- poj 3257 二维dp
- uva10313(二维多重DP)
- HDU1176二维DP
- HDU3127 二维完全背包 DP
- hdu 2845 Beans--二维DP
- POJ 2948 经典二维dp
- hdu 2859 (二维dp)
- 中断服务程序(Interrupt Service Routines,ISR)注意事项
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- Android之文件浏览器
- kaptcha验证码使用
- java对象的强引用,软引用,弱引用和虚引用
- 二维dp
- 2014.08.08 centos6.5+mysql6.5
- 完全跨域的单点登录
- Android 带checkbox的listView 实现多选,全选,反选
- 文件上传利器SWFUpload使用指南
- C#项目启动调试时没有重新生成项目
- 南阳 7 街区最短路径问题 (中位数)
- 浅析 Java Thread.join()
- 亚马逊云aws12个月免费服务器搭建小结