题目传送门

题意:给出一些砖头的长宽高,砖头能叠在另一块上要求它的长宽都小于下面的转头的长宽,问叠起来最高能有多高

分析:设一个砖头的长宽高为x, y, z,那么想当于多了x, z, y 和y, x, z的砖头,如果i能叠在j上,那么g[i][j] = true,转换成DAG问题,dp[i]表示第i块叠在最上部最高的高度

收获:转换成经典模型

代码:

/************************************************
* Author :Running_Time
* Created Time :2015-8-28 18:00:01
* File Name :UVA_437.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Block {
int x, y, z;
}b[N];
bool g[N][N];
int dp[N];
int n; int DFS(int u) {
if (dp[u] != -1) return dp[u];
dp[u] = b[u].z;
for (int i=1; i<=n; ++i) {
if (g[u][i]) {
dp[u] = max (dp[u], DFS (i) + b[u].z);
}
}
return dp[u];
} bool check(int i, int j) {
if (b[i].x < b[j].x && b[i].y < b[j].y) return true;
if (b[i].x < b[j].y && b[i].y < b[j].x) return true;
return false;
} int main(void) {
int cas = 0;
while (scanf ("%d", &n) == 1) {
if (n == 0) break;
for (int i=1; i<=n; ++i) {
scanf ("%d%d%d", &b[i].x, &b[i].y, &b[i].z);
b[n+i].x = b[i].x, b[n+i].y = b[i].z, b[n+i].z = b[i].y;
b[2*n+i].x = b[i].y, b[2*n+i].y = b[i].z, b[2*n+i].z = b[i].x;
}
memset (g, false, sizeof (g));
n *= 3;
for (int i=1; i<=n; ++i) {
for (int j=i+1; j<=n; ++j) {
if (check (i, j)) g[i][j] = true;
if (check (j, i)) g[j][i] = true;
}
}
memset (dp, -1, sizeof (dp));
int ans = 0;
for (int i=1; i<=n; ++i) {
ans = max (ans, DFS (i));
}
printf ("Case %d: maximum height = %d\n", ++cas, ans);
} return 0;
}

  

DP(DAG) UVA 437 The Tower of Babylon的更多相关文章

  1. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

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

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

  3. UVa 437 The Tower of Babylon

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

  4. UVA 437 The Tower of Babylon(DAG上的动态规划)

    题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度. 方法1,建图求解,可以把问题转化成求DAG上的最长路问题 #include <cstdio> #include &l ...

  5. UVA 437 "The Tower of Babylon" (DAG上的动态规划)

    传送门 题意 有 n 种立方体,每种都有无穷多个. 要求选一些立方体摞成一根尽量高的柱子(在摞的时候可以自行选择哪一条边作为高): 立方体 a 可以放在立方体 b 上方的前提条件是立方体 a 的底面长 ...

  6. UVA - 437 The Tower of Babylon(dp-最长递增子序列)

    每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...

  7. UVA 437 The Tower of Babylon巴比伦塔

    题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...

  8. UVA 427 The Tower of Babylon 巴比伦塔(dp)

    据说是DAG的dp,可用spfa来做,松弛操作改成变长.注意状态的表示. 影响决策的只有顶部的尺寸,因为尺寸可能很大,所以用立方体的编号和高的编号来表示,然后向尺寸更小的转移就行了. #include ...

  9. UVA 437 十九 The Tower of Babylon

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

随机推荐

  1. 【转】c++内存管理学习纲要

    http://blog.csdn.net/zhanghefu/article/details/5003407 转自:http://blog.csdn.net/wdzxl198/article/deta ...

  2. composer-安装laravel

    laravel文档地址: https://docs.golaravel.com/docs/5.6/installation/ 我们怎么将Apache和PHP互联起来呢? http://www.cnbl ...

  3. Linux的基本优化

    归结成口诀: 一清.一精.一增.两优.四设.七其他 一清: 定时清理日志/var/spool/clientsqueue 一精: 精简开机启动服务 一增: 增大文件描述符 两优: linux内核参数的优 ...

  4. notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比方lua、python等)

    在程序开发过程中,程序猿经经常使用到的一个功能就是查找引用(Find Reference).Visual Studio里面的相应功能是"查找全部引用"(Find All Refer ...

  5. Spring中注解

    @Autowired :spring注解 @Resource :J2EE注解 @Transactional(rollbackFor=Exception.class):指定回滚 @RequestMapp ...

  6. react-grid-layout

    一个好用的拖拽.自适应布局 react 插件 基本使用: // 显示全部 chart 内容区域 import React,{PureComponent} from 'react'; import {R ...

  7. java实用技能 上传文件 等等

    1.IOS  AES对称加密,加密结果不同,问题解决 IOS http post请求,使用AFNetworing 框架,默认请求content-type为application/json ,所以无法使 ...

  8. sphinx测试数据生成

    import json from random import sample, randint from uuid import uuid4 def gen_random_words(): with o ...

  9. Eclipse 插件管理

    查看已安装的插件: [help]⇒ [About Eclipse]⇒ [Installed Softwares] 1. 常用插件 maven:安装步骤如下: [help]⇒ [Install new ...

  10. x86 linux 裁剪过程中能正常跑起来的必要配置项

    A .选中Executable file formats/Emulations ---> Kernel support for ELFbinaries -----加载运行rootfs 中的程序. ...