题目:The Tower of Babylon

这是一个DAG 模型,有两种常规解法

1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归

2.递推法

方法一:记忆化搜索
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,z;
node(int x=0,int y=0,int z=0) : x(x) , y(y) , z(z){}
}exa[5000]; bool cmp(node a,node b)
{
if(a.x==b.x) return a.y>b.y;
else return a.x>b.x;
} int n;
int dp[10000];
bool vis[10000]; int d(int e)
{
if(vis[e]) return dp[e];
vis[e]=1;
int &ans=dp[e];
ans=exa[e].z;
for(int i=1;i<e;i++)
{
// printf("exa[%d].x=%d\n",i,exa[i].x);
// printf("exa[%d].y=%d\n",i,exa[i].y);
// printf("exa[%d].x=%d\n",e,exa[e].x);
// printf("exa[%d].y=%d\n",e,exa[e].y);
if(exa[i].x>exa[e].x&&exa[i].y>exa[e].y)
{
ans=max(ans,d(i)+exa[e].z);
// cout<<ans<<" "<<i<<endl;
}
}
return ans;
} int main()
{
int num=0;
while(scanf("%d",&n)==1&&n)
{
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
exa[i]=node(a,b,c);
exa[n+i]=node(b,c,a);
exa[n*2+i]=node(b,a,c);
exa[n*3+i]=node(a,c,b);
exa[n*4+i]=node(c,a,b);
exa[n*5+i]=node(c,b,a);
}
n*=6;
sort(exa+1,exa+n+1,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{
int tmp=d(i);
ans=max(ans,tmp);
}
printf("Case %d: maximum height = %d\n",++num,ans);
}
return 0;
}

  

方法二:递推法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,z;
node(int x=,int y=,int z=) : x(x) , y(y) , z(z){}
}exa[];
bool cmp(node a,node b)
{
if(a.x==b.x) return a.y>b.y;
else return a.x>b.x;
} int n;
int dp[]; int main()
{
int num=;
while(scanf("%d",&n)==&&n)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
exa[i]=node(a,b,c);
exa[n+i]=node(b,c,a);
exa[n*+i]=node(b,a,c);
exa[n*+i]=node(a,c,b);
exa[n*+i]=node(c,a,b);
exa[n*+i]=node(c,b,a);
}
n*=;
int ans=;
sort(exa+,exa+n+,cmp);
for(int i=;i<=n;i++)
{
dp[i]=exa[i].z;
for(int j=;j<i;j++)
{
if(exa[i].x<exa[j].x&&exa[i].y<exa[j].y)
{
dp[i]=max(dp[i],dp[j]+exa[i].z);
//cout<<dp[i]<<"ww "<<i<<endl;
}//cout<<dp[i]<<" "<<i<<endl;
}
ans=max(ans,dp[i]);
}
printf("Case %d: maximum height = ",++num);
cout<<ans<<endl;
}
return ;
}

DAG 动态规划 巴比伦塔 B - The Tower of Babylon的更多相关文章

  1. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  2. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  3. [动态规划]UVA437 - The Tower of Babylon

     The Tower of Babylon  Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...

  4. ACM - 动态规划 - UVA437 The Tower of Babylon

    UVA437 The Tower of Babylon 题解 初始时给了 \(n\) 种长方体方块,每种有无限个,对于每一个方块,我们可以选择一面作为底.然后用这些方块尽可能高地堆叠成一个塔,要求只有 ...

  5. UVA The Tower of Babylon

    The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many det ...

  6. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  7. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  8. POJ 2241 The Tower of Babylon

    The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Or ...

  9. Uva437 The Tower of Babylon

    https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965 Perhaps you have heard of ...

随机推荐

  1. mysql主从复制总结

    第一步:开启所有MYSQL服务器的BIN日志,每台服务器设置一个唯一的server-id的值(默认是1,一般取IP最后一段) 修改主服务器(master)的my.cnf [mysqld] log-bi ...

  2. JavaWeb学习 (十一)————Session

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  3. C# DataGrid 用法---极速入门测试

    目标: 新手编程,只求DataGrid能运行起来,更多功能留在后面探讨. 步骤: 1.新建WPF文档 插入DataGrid控件. <Window x:Class="OASevl.Mai ...

  4. [NOI 2015]品酒大会

    Description 题库链接 \(n\) 杯鸡尾酒排成一行,其中第 \(i\) 杯酒 (\(1 \leq i \leq n\)) 被贴上了一个标签 \(s_i\),每个标签都是 \(26\) 个小 ...

  5. [Luogu 3401] 洛谷树

    Description 有一棵树,要求支持 查询两点间简单路径的所有子链的异或和的和 修改某条边的权值 Solution 这种树上异或问题首先应该想到对于每个点存下一个前缀异或和表示这个点到根节点路径 ...

  6. Java Generator

    以前我以为只有Python才会有generator,看来当时的我才年轻,后来认真研读<Thinking in Java>之后大有感悟,原来Java亦有generator,故做一次记录分享. ...

  7. js 去掉缓存的几种方式

    1.在Ajax发送请求前加上 anyAjaxObj.setRequestHeader ("If-Modified-Since","0") 2.在Ajax发送请求 ...

  8. Java集合框架——容器的快速报错机制 fail-fast 是什么?

    前言:最近看 java 集合方面的源码,了解到集合使用了 fail-fast 的机制,这里就记录一下这个机制是什么,有什么用,如何实现的. 一.fail-fast 简介 fail-fast 机制,即快 ...

  9. Android系统常用URI

    android系统常用URI android系统管理联系人的URI如下: ContactsContract.Contacts.CONTENT_URI 管理联系人的Uri ContactsContrac ...

  10. canvas-2arcTo.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...