题目传送门

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

分析:设一个砖头的长宽高为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. iphone5s 耳机更换插头 EarPods change jack

    iphone5s 耳机使用了不到两年,出现了接头接触不良,话筒线短路的状况,经常自动出现暂停或者siri.买了一个新耳机,这几天有时间,把旧耳机修好了,更换了一个新的插头.   工具/原料   剥线钳 ...

  2. 【求建议】毕业之声——信院IT类毕业学子经验分享交流会

    一:缘由 在和非常多学子交流,及上课的经历中,发现一个非常普遍的现象:部分大一学生即失去了对学习.对专业的兴趣.有人在迷茫之后奋起直追.从而珍惜利用不多的大学时光努力提高自己.有人在迷茫中沉沦,沉迷于 ...

  3. D广搜

    <span style="color:#330099;">/* D - 广搜 基础 Time Limit:1000MS Memory Limit:30000KB 64b ...

  4. java克隆对象clone()的使用方法和作用

    转自:997.html">http://www.okrs.cn/blog/news/?997.html 内容摘要 若需改动一个对象,同一时候不想改变调用者的对象.就要制作该对象的一个本 ...

  5. python实现接口自动化

    一.总述 Postman:功能强大,界面好看响应格式自主选择,缺点支持的协议单一且不能数据分离,比较麻烦的还有不是所有的公司都能上谷歌SoupUI:支持多协议(http\soup\rest等),能实现 ...

  6. POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)

    Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 3864   S ...

  7. Vi/Vim查找替换使用方法【转】

    原文地址:http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂 ...

  8. leetCode(26):Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. 可以声明接口,但不可以new接口

    接口是一种特殊的抽象类,它包含常量和方法的声明,但没有方法的实现:可以把接口看成是一种特殊的抽象类: 接口实质上是一种规范,它关心的是"做什么",不关心"怎样做" ...

  10. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...