hdu 4001(dp)

To Miss Our Children Time

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3621    Accepted Submission(s): 946


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
 

Source
 

Recommend
lcy
 

首先,对于d是0的,a,b相同的,用map<pair<a,b>> 记录下所有c的和,对于d是1或2的,ab相同的,就记录下c的最大值。这样对于每个blocks,能覆盖另外一个blocks的,就连一条有向边,这样就得到了一个dag图,最后在dag图上找权和最大的路就是答案。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 1000000000;
typedef long long LL;
typedef pair<int,int> P;

struct Node{
    LL a,b,c,d;
}num[maxn];

map<P,LL> M[3];
vector<int> G[maxn];
LL dp[maxn];
int cnt;

LL Dfs(int x){
    if(dp[x] != -1) return dp[x];
    LL Max = 0;

    for(int i = 0;i < G[x].size();i++){
        int to = G[x][i];
        Max = max(Max,Dfs(to));
    }
    Node tem = num[x];
    LL der = M[tem.d][P(tem.a,tem.b)];
    return dp[x] = Max + der;
}

LL ans;

void Dp(){
    ans = -1;
    for(int i = 0;i < cnt;i++){
        Dfs(i);
        ans = max(ans,dp[i]);
    }
}

int main(){
    int n;
    LL a,b,c,d;
    while(scanf("%d",&n) != EOF){
        if(n == 0) break;
        M[0].clear();M[1].clear();M[2].clear();
        cnt = 0;
        for(int i = 0;i < n;i++) G[i].clear();
        for(int i = 0;i < n;i++){
            scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d);
            if(a > b) swap(a,b);
            if(M[d].count(P(a,b)) == 0) num[cnt++] = (Node){a,b,c,d};
            if(d == 0) M[0][P(a,b)] += c;
            else M[d][P(a,b)] = max(c,M[d][P(a,b)]);
        }
        for(int i = 0;i < cnt;i++){
            for(int j = 0;j < cnt;j++){
                if(i == j) continue;
                if(num[i].d == 0){
                    if(num[i].a >= num[j].a && num[i].b >= num[j].b) G[j].push_back(i);
                }
                else if(num[i].d == 1){
                    if(num[i].a >= num[j].a && num[i].b >= num[j].b && (num[i].a != num[j].a || num[i].b != num[j].b)){
                        G[j].push_back(i);
                    }
                }
                else{
                    if(num[i].a > num[j].a && num[i].b > num[j].b) G[j].push_back(i);
                }
            }
        }
        memset(dp,-1,sizeof(dp));
        Dp();
        cout << ans << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值