https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi , yi , zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. They wanted to construct the tallest tower possible by stacking blocks. The problem was that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn’t be stacked. Your job is to write a program that determines the height of the tallest tower the babylonians can build with a given set of blocks. Input The input file will contain one or more test cases. The first line of each test case contains an integer n, representing the number of different blocks in the following data set. The maximum value for n is 30. Each of the next n lines contains three integers representing the values xi , yi and zi . Input is terminated by a value of zero (0) for n. Output For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format ‘Case case: maximum height = height’ Sample Input 1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0 Sample Output Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342

【题解】

考虑a,b,c三个长度哪一个做高,分别用1/2/3表示

dp[i][1/2/3]表示i在最上面,a/b/c作高的最大高度

其实是一个DAG

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const int MAXN = + ; struct Node
{
int a,b,c;
int geta(int num)
{
if(num == )return a;
else if(num == )return b;
else return c;
}
int getb(int num)
{
if(num == )return b;
else if(num == )return c;
else return a;
}
int getc(int num)
{
if(num == )return c;
else if(num == )return a;
else return b;
}
}node[MAXN]; int tt, dp[MAXN][MAXN], n; int cmp(Node a, int numa, Node b, int numb)
{
return (a.geta(numa) < b.geta(numb) && a.getb(numa) < b.getb(numb)) || (a.geta(numa) < b.getb(numb) && a.getb(numa) < b.geta(numb));
} int f(int a, int b)
{
if(dp[a][b] != -)return dp[a][b];
dp[a][b] = node[a].getc(b);
for(register int i = ;i <= n;++ i)
{
for(register int j = ;j <= ;++ j)
{
if(cmp(node[i],j,node[a],b))
dp[a][b] = max(dp[a][b], f(i, j) + node[a].getc(b));
}
}
return dp[a][b];
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
++ tt;
memset(dp, -, sizeof(dp));
for(register int i = ;i <= n;++ i)
read(node[i].a), read(node[i].b), read(node[i].c);
int ans = -;
for(register int i = ;i <= n;++ i)
for(register int j = ;j <= ;++ j)
ans = max(ans, f(i, j));
printf("Case %d: maximum height = %d\n", tt, ans);
}
return ;
}

Uva437

Uva437 The Tower of Babylon的更多相关文章

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

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

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

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

  3. UVa437 The Tower of Babylon(巴比伦塔)

    题目 有n(n<=30)种立方体,每种有无穷多个,摞成尽量高的柱子,要求上面的立方体要严格小于下面的立方体. 原题链接 分析 顶面的大小会影响后续的决策,但不能直接用d[a][b]来表示,因为可 ...

  4. 【DP】【Uva437】UVA437 The Tower of Babylon

    传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...

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

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

  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 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  8. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

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

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

随机推荐

  1. MyEclipse使用总结——在MyEclipse中新建Maven框架的web项目[转]

    前面的文章我们已经在本机安装好了maven,同时在myeclipse中配置好了maven的插件. 链接如下: Maven安装----在Windows上安装Maven myeclipse安装maven插 ...

  2. Installer - win10安装及卸载SQL Server2008数据库

    一.数据库安装环境 操作系统:win10 SQL server:SQL server 2008 R2 二.全新数据库安装 1.安装扩展文件 双击安装文件,弹出如下窗体:                 ...

  3. 模板——二分图匹配KM

    具体方法就不介绍了,详见 https://blog.csdn.net/sixdaycoder/article/details/47720471 主要讲一些注意点: 1:不直接将未匹配的y减小是因为要保 ...

  4. elasticsearch 中文API 删除(四)

    删除API 删除api允许你通过id,从特定的索引中删除类型化的JSON文档.如下例: DeleteResponse response = client.prepareDelete("twi ...

  5. 初识splay

    这东西都没什么板子着实让我很难受啊,只能到网上抄抄补补, 记下两个用到的博客 https://blog.csdn.net/clove_unique/article/details/50630280 h ...

  6. 高德地图定位不到 报错 location Error, ErrCode:7, errInfo:KEY错误 请到http://lbs.amap.com/api/android-location-sdk/abouterrorcode/查看错误码说明.

    出现该问题的可能是高德地图的配置不准确: 仔细配对一下 看sha1 是否是通过应用签名生成的  要区分发布版的sha1 跟调试版的sha1  是不相同的 (小编我第一次反这种错误的时候 是因为我把高得 ...

  7. win10 安装face_recongnition

    1.安装dlib https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10 2.安装face_recon ...

  8. centos的yum配置

    什么是yum ?yum,是Yellow dog Updater Modified的简称,起初是由yellow dog这一发行版的开发者Terra Soft研发,用python写成,那时还叫做yup(y ...

  9. 快速傅里叶变换(FFT)学习

    首先,在写这篇博客之前,我还没有完全学会FFT. 先把会的部分打好,加深一下记忆(也可以说是做笔记吧). 初三了,还不会FFT,要退役喽-- 多项式乘法 点开这篇博客之前,你就应该知道,FFT是用来求 ...

  10. php 支付宝新版本app支付以及回调

    ;支付宝快速接入; 支付宝2017年新版本支付基本业务逻辑 服务端生成字符串 交给客户端, 客户端调用接口,将这段字符串str传过去 调用起支付界面. 其中字符串str包含了所有请求参数,以及请求参数 ...