UVA 437 The Tower of Babylon (记忆化搜dp)

18 篇文章 0 订阅

题意:有集中长方体,将其摞在一起,上面的底面面积要严格小于下面,每种长方体可以无限次使用,问最高可以摆放多高

分析:DAG最长路算法,将一个长方体当成3个长方体使用,不用建边,转移的时候判断一下大小关系即可,最后dfs

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define FRER() freopen("in.txt","r",stdin)
#define FREW() freopen("out.txt","w",stdout)
#define go int T;cin>>T;while(T--)
#define debug cout<<"****************"<<endl
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn = 10000 + 7,inf = 0x3f3f3f3f;
int n,tot;
int dp[maxn];
bool vis[maxn][3];
struct REC{
    int a,b,c;
    bool operator < (const REC& rhs) const {
        return a*c < rhs.a*rhs.c;
    }
}r[maxn];
int dfs(int idx){
    int& ans = dp[idx];
    if(ans) return ans;
    ans = r[idx].b;
    for(int i=0;i<tot;i++){
        if((r[i].a<r[idx].a&&r[i].c<r[idx].c)||(r[i].c<r[idx].a&&r[i].a<r[idx].c))
            ans = max(ans,dfs(i)+r[idx].b);
    }
    return ans;
}
int main(){
    //FRER();
    //FREW();
    int k = 1;
    while(scanf("%d",&n)!=EOF&&n){
        memset(dp, 0, sizeof(dp));
        tot = 0;
        for(int i=0;i<n;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            r[tot].a = a;r[tot].b=b;r[tot++].c=c;
            r[tot].a = a;r[tot].b=c;r[tot++].c=b;
            r[tot].a = b;r[tot].b=a;r[tot++].c=c;
        }
        int ans = 0;
        for(int i=0;i<tot;i++){
            ans = max(ans,dfs(i));
        }
        printf("Case %d: maximum height = %d\n",k++,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值