hdu3127WHUgirls【二维完全背包】

来源:互联网 发布:qq输入法linux 编辑:程序博客网 时间:2024/05/16 18:39

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
12 4 42 2 23 3 9
 

Sample Output
9

就这么一个小破题 写了三天→_→

之前还想简单了(说白了就是写题没走脑子)

这个就是DP思路~~分块了而已 自己改了一遍也没对 WA的代码:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,X,Y,t;int dp[1005][1005],x[12],y[12],c[12];int max(int a,int b,int c){    if(a>b)    {        if(c>a) return c;        return a;    }    if(c>b) return c;    return b;}int main(){    //freopen("cin.txt","r",stdin);    cin>>t;    while(t--)    {        cin>>n>>X>>Y;        for(int i=1;i<=n;i++) cin>>x[i]>>y[i]>>c[i];        memset(dp,0,sizeof(dp));        for(int k=1;k<=n;k++)        {        for(int i=0;i<=X;i++)        {            for(int j=0;j<=Y;j++)            {                    if(i>=x[k]&&j>=y[k])                    dp[i][j]=max(dp[i][j],dp[i-x[k]][j]+dp[x[k]][j-y[k]]+c[k],dp[i][j-y[k]]+dp[i-x[k]][y[k]]+c[k]);                    if(i>=y[k]&&j>=x[k])                    dp[i][j]=max(dp[i][j],dp[i-y[k]][j]+dp[y[k]][j-x[k]]+c[k],dp[i][j-x[k]]+dp[i-y[k]][x[k]]+c[k]);                }            }        }        cout<<dp[X][Y]<<endl;    }    return 0;}

为啥错呢?想了一下 dp的前后关系不是由方框的序号来的,只是由x,y的坐标值推导出来的~~所以大循环是坐标!

AC代码:

/*************hdu31272015.10.27-2015.10.29296MS 5536K 1130 B************/#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,X,Y,t;int dp[1005][1005],x[12],y[12],c[12];int max(int a,int b,int c){    if(a>b)    {        if(c>a) return c;        return a;    }    if(c>b) return c;    return b;}int main(){    //freopen("cin.txt","r",stdin);    cin>>t;    while(t--)    {        cin>>n>>X>>Y;        for(int i=1;i<=n;i++) cin>>x[i]>>y[i]>>c[i];        memset(dp,0,sizeof(dp));        for(int k=1;k<=n;k++)        {            for(int i=0;i<=X;i++)            {                for(int j=0;j<=Y;j++)                {                    if(i>=x[k]&&j>=y[k])                    dp[i][j]=max(dp[i][j],dp[i-x[k]][j]+dp[x[k]][j-y[k]]+c[k],dp[i][j-y[k]]+dp[i-x[k]][y[k]]+c[k]);                    if(i>=y[k]&&j>=x[k])                    dp[i][j]=max(dp[i][j],dp[i-y[k]][j]+dp[y[k]][j-x[k]]+c[k],dp[i][j-x[k]]+dp[i-y[k]][x[k]]+c[k]);                }            }        }        cout<<dp[X][Y]<<endl;    }    return 0;}



0 0
原创粉丝点击