HDU 4001 To Miss Our Children Time(DAG上的DP)

To Miss Our Children Time

Problem Description
Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy, now he grows up. In the dead of night, he often misses something, including a simple game which brings him much happy when he was child. Here are the game rules: There lies many blocks on the ground, little LLL wants build "Skyscraper" using these blocks. There are three kinds of blocks signed by an integer d. We describe each block's shape is Cuboid using four integers ai, bi, ci, di. ai, bi are two edges of the block one of them is length the other is width. ci is
thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
 

Input
The input has many test cases.
For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks.
From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2).
The input end with n = 0.
 

Output
Output a line contains a integer describing the highest "Skyscraper"'s height using the n blocks.
 

Sample Input
  
  
3 10 10 12 0 10 10 12 1 10 10 11 2 2 10 10 11 1 10 10 11 1 0
 

Sample Output
  
  
24 11
 
【思路分析】
   题目的意思是有3种若干个长方体木块(种类号为0、1、2),设其长、宽、高和类型分别为ai,bi,ci,di。它们可以按以下规则摞在别的木块之上:
1、di为0的长方体,当其长宽分别大于或等于某个长方体时,可以摆放在某个长方体上面。
2、di为1的长方体,当其长宽分别大于或等于且其底面积大于某个长方体时,可以摆放在某个长方体上面。
3、di为2的长方体,当其长宽分别大于某个长方体时,可以摆放在某个长方体上面。
要求这些长方体最多可以摞多高?
   由题意可知长方体之间存在着上面和下面的二元关系,这类似于之前嵌套矩形的问题。因此可以将此问题转化为求DAG上的最长路径问题,但是需要注意的一点是,嵌套矩形中不存在环的情况(即当一个矩形的长宽严格小于另一个矩形的长宽时才能被其嵌套)。此题三个规则中的规则一存在大于等于的关系,即一个长方体的长宽和另一个长方体的长宽相等并且类型相同时,二者可以相互放在对方之上,这便产生了环并且显然是矛盾的。因此在预处理的过程中,把两个满足上述条件的长方体合并为一个,即规定将第二个长方体放在第一个长方体之上,同时用下一个长方体“覆盖”这第二个长方体,这样就可以消除建立的图中存在环的情况了。

代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int n,num;
int DAG[maxn][maxn];
long long d[maxn];
struct Cuboid
{
    long long a,b,c,d;
}cuboid[maxn];

void creatGraph(int x,int y)
{
    if(cuboid[x].d == 0 && cuboid[x].a >= cuboid[y].a && cuboid[x].b >= cuboid[y].b)
    {
        DAG[x][y] = 1;
    }
    if(cuboid[x].d == 1 && cuboid[x].a >= cuboid[y].a && cuboid[x].b >= cuboid[y].b &&
       cuboid[x].a * cuboid[x].b > cuboid[y].a * cuboid[y].b)
    {
        DAG[x][y] = 1;
    }
    if(cuboid[x].d == 2 && cuboid[x].a > cuboid[y].a && cuboid[x].b > cuboid[y].b)
    {
        DAG[x][y] = 1;
    }
}
long long dp(int i)//记忆化搜索
{
    long long ans = d[i];
    if(ans > 0)
        return ans;
    ans = cuboid[i].c;
    for(int j = 0;j < n;j++)
    {
        if(DAG[i][j])
        {
            long long temp = dp(j);
            ans = ans > (temp + cuboid[i].c) ? ans : (temp + cuboid[i].c);
        }
    }
    return d[i] = ans;
}
void init()
{
    memset(DAG,0,sizeof(DAG));
    memset(d,0,sizeof(d));
    int i = 0;
    for(int t = 0;t < n;t++)
    {
        scanf("%I64d %I64d %I64d %I64d",&cuboid[i].a,&cuboid[i].b,&cuboid[i].c,&cuboid[i].d);
        if(cuboid[i].a < cuboid[i].b)//边输入边预处理
        {
            long long temp = cuboid[i].a;
            cuboid[i].a = cuboid[i].b;
            cuboid[i].b = temp;
        }
        int flag = 1;
        for(int j = 0;j < i;j++)
        {
            if(cuboid[i].d == 0 && cuboid[j].d == 0)
            {
                if(cuboid[i].a == cuboid[j].a && cuboid[i].b == cuboid[j].b)//处理存在环的情况
                {
                    cuboid[j].c += cuboid[i].c;//将长宽和类型相同的长方体合并为一个
                    flag = 0;//二者合并为一个后,下一个长方体会“覆盖”掉长方体cuboid[i]
                }
            }
        }
        if(flag == 1)
        {
            for(int j = 0;j < i;j++)
            {
                creatGraph(j,i);
                creatGraph(i,j);//两种情况
            }
        }
        i += flag;
    }
    num = i;//合并之后长方体的总数
}
void solve()
{
    long long ans = 0;
    for(int i = 0;i < num;i++)
    {
        long long temp = dp(i);
        ans = ans > temp ? ans : temp;
    }
    printf("%I64d\n",ans);
}
int main()
{
    while(scanf("%d",&n) && n != 0)
    {
        init();
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值