DP(DAG) UVA 437 The Tower of Babylon
题意:给出一些砖头的长宽高,砖头能叠在另一块上要求它的长宽都小于下面的转头的长宽,问叠起来最高能有多高
分析:设一个砖头的长宽高为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的更多相关文章
- UVa 437 The Tower of Babylon(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...
- UVa 437 The Tower of Babylon(经典动态规划)
传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...
- UVa 437 The Tower of Babylon
Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...
- UVA 437 The Tower of Babylon(DAG上的动态规划)
题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度. 方法1,建图求解,可以把问题转化成求DAG上的最长路问题 #include <cstdio> #include &l ...
- UVA 437 "The Tower of Babylon" (DAG上的动态规划)
传送门 题意 有 n 种立方体,每种都有无穷多个. 要求选一些立方体摞成一根尽量高的柱子(在摞的时候可以自行选择哪一条边作为高): 立方体 a 可以放在立方体 b 上方的前提条件是立方体 a 的底面长 ...
- UVA - 437 The Tower of Babylon(dp-最长递增子序列)
每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...
- UVA 437 The Tower of Babylon巴比伦塔
题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...
- UVA 427 The Tower of Babylon 巴比伦塔(dp)
据说是DAG的dp,可用spfa来做,松弛操作改成变长.注意状态的表示. 影响决策的只有顶部的尺寸,因为尺寸可能很大,所以用立方体的编号和高的编号来表示,然后向尺寸更小的转移就行了. #include ...
- UVA 437 十九 The Tower of Babylon
The Tower of Babylon Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
随机推荐
- JVM监控工具:jps、jstat、jinfo、jmap、jhat、jstack使用介绍
转载:http://outofmemory.cn/java/jvm/jvm-tools-jps-jstat-jinfo-jmap-jhat-jstack 一.jps(JVM Process Statu ...
- TeamCity - Docker创建
// 创建Server docker run -it --name teamcity-server-instance \-v /home/tc_datadir:/data/teamcity_serve ...
- iptables防火墙以及网络协议基本原理
一. Linux 网络安全模型 1. 防火墙: 工作在主机或者网络边缘,对进出报文使用实现定义的规则进行检测,并且由匹配的规则进行处理的一组硬件或者软件.也可能两者结合. 1) 通常使用的防火墙设备 ...
- linux用户列表
centos上面不知道添加了多少个账户,今天想清理一下,但是以前还未查看过linux用户列表, 一般情况下是 cat /etc/passwd 可以查看所有用户的列表 w 可以查看当前活跃的用户列表 c ...
- 2003 -Can't connection to mysql server on | navicat for mysql Access denied for user 'root'@''ip'(using password :yes)
用本机windows上的Navicat for mysql链接虚拟机Linux的mysql数据库时,第一次连接的时候报的错误是 2003 -Can't connection to mysql serv ...
- 用SQL脚本 生成INSERT SQL语句
配置表B 中的数据,可以从A表中查询到,在实际配置时,通过sql脚本生成B表的insert脚本,最多用到的是sql中连接符[||],以及双引号[''''] 例1:电销系统中地区出单机构关系表配置数据生 ...
- 【Linux编程】进程终止和exit函数
内核要运行一个应用程序,唯一的途径是通过系统调用.exec函数.exec又会调用启动程序,启动程序(一般是汇编语言)以类似以下的方式调用main函数: void exit(main(argc, arg ...
- vmware10上安装mac os 10.9
来源地址:http://dtbuluo.com/blog/archives/350 序言: 前几天跟朋友开玩笑说,要不我们一起来学习一下swift编程语言吧~我们就抱着玩玩的态度,没有想过要做出什么优 ...
- 比 git log 更强大的 git reflog
最近做了个骚操作 git checkout commitId 修改了部分内容 git add . git commit -m '修改了些东西' -> 此时git 会自动生成一个新的 comm ...
- Hibernate中Criteria的完整用法?
http://www.cnblogs.com/mabaishui/archive/2009/10/16/1584510.html