题目传送门

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

分析:设一个砖头的长宽高为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. 从ASP.NET Core 3.0 preview 特性,了解CLR的Garbage Collection

    前言 在阅读这篇文章:Announcing Net Core 3 Preview3的时候,我看到了这样一个特性: Docker and cgroup memory Limits We conclude ...

  2. 设计模式 之代理(Proxy)模式

    为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...

  3. 关键路径(AOE)---《数据结构》严蔚敏

    // exam1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  4. SPOJ QTREE6 lct

    题目链接 岛娘出的题.还是比較easy的 #include <iostream> #include <fstream> #include <string> #inc ...

  5. 【CODEFORCES】 C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. [Tue, 11 Aug 2015 ~ Mon, 17 Aug 2015] Deep Learning in arxiv

    Image Representations and New Domains inNeural Image Captioning we find that a state-of-theart neura ...

  7. Class.forName() 详解

    主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...

  8. XMU C语言程序设计实践(2)

    任务一:颠倒的世界 小明最近突然喜欢倒着写字,写出来的句子全是颠倒的,也就是把一句话里的字符全都逆序写,譬如“I Love This Game!”,他就偏偏要写成“!emaG sihT evoL I” ...

  9. How to Execute Page_Load() in Page's Base Class?

    https://stackoverflow.com/questions/2737092/how-to-execute-page-load-in-pages-base-class We faced th ...

  10. YTU 2441: C++习题 复数类--重载运算符2+

    2441: C++习题 复数类--重载运算符2+ 时间限制: 1 Sec  内存限制: 128 MB 提交: 847  解决: 618 题目描述 定义一个复数类Complex,重载运算符"+ ...