UVA 437 The Tower of Babylon(DAG上的动态规划)
题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度。
方法1,建图求解,可以把问题转化成求DAG上的最长路问题
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = ;
struct Node{
int x;
int y;
int z;
Node(int x,int y,int z):x(x),y(y),z(z){}
Node(){}
bool operator < (const Node &n)const{
return (x < n.x&& y < n.y) || (x < n.y&& y < n.x);
}
};
vector<Node> vec;
int n;
int d[maxn*];
int G[maxn*][maxn*];
int dp(int i,int h){
int & ans = d[i];
if(ans>)return ans;
ans = h;
for(int j = ; j < n*; j++)if(G[i][j]){
ans = max(ans,dp(j,vec[j].z)+h);
}
return ans;
}
int main(){
int cnt = ;
while(scanf("%d",&n)==&&n){
vec.clear();
memset(G,,sizeof(G));
memset(d,,sizeof(d));
for(int i = ; i< n; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
vec.push_back(Node(x,y,z));
vec.push_back(Node(x,z,y));
vec.push_back(Node(z,y,x));
}
sort(vec.begin(),vec.end()); for(int i = ;i < n*; i++){
for(int j = ; j < n*; j++){
if(vec[i] < vec[j])
G[i][j] = ;
}
}
int result = -;
for(int i = ; i < n* ;i++){
result = max(result,dp(i,vec[i].z));
}
printf("Case %d: maximum height = %d\n",++cnt,result);
}
return ;
}
方法2,转化成最长递增子序列问题求解
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = ;
struct Node{
int x;
int y;
int z;
Node(int x,int y,int z):x(x),y(y),z(z){}
Node(){}
bool operator < (const Node &n)const{
return (x < n.x&& y < n.y) || (x < n.y&& y < n.x);
}
};
bool cmp(Node a,Node b){
return a.x*a.y < b.x*b.y;
}
vector<Node> vec;
int n;
int d[maxn*];
int LIS(int n){
//d[i] = max{hi,d[j]}i>j ,A[i]>A[j]
int t = ;
for(int i = ; i < n*; i++){
d[i] = vec[i].z;
for(int j = ; j < i; j++)
if(vec[j] < vec[i])
d[i] = max(d[i],d[j]+vec[i].z);
if(d[i] > t) t = d[i];
}
return t;
}
int main(){
int cnt = ;
while(scanf("%d",&n)==&&n){
vec.clear();
memset(d,,sizeof(d));
for(int i = ; i< n; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
vec.push_back(Node(x,y,z));
vec.push_back(Node(x,z,y));
vec.push_back(Node(z,y,x));
}
sort(vec.begin(),vec.end(),cmp);
printf("Case %d: maximum height = %d\n",++cnt,LIS(n));
}
return ;
}
方法二要注意一点,我sort序列的时候用了一个cmp函数,他是根据立方体的底面积对立方体进行排序的。为什么不采用和方法一一样的排序方式呢?
因为a.x <b.x&&a.y<b.y
或 a.y<b.x&&a.x<b.y
=>Sa <Sb
所以在塔上面的立方体的底面积一定比下面的小。
UVA 437 The Tower of Babylon(DAG上的动态规划)的更多相关文章
- 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(DP 最长条件子序列)
题意 给你n种长方体 每种都有无穷个 当一个长方体的长和宽都小于还有一个时 这个长方体能够放在还有一个上面 要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法 比較不好控制 ...
- UVA 437 "The Tower of Babylon" (DAG上的动态规划)
传送门 题意 有 n 种立方体,每种都有无穷多个. 要求选一些立方体摞成一根尽量高的柱子(在摞的时候可以自行选择哪一条边作为高): 立方体 a 可以放在立方体 b 上方的前提条件是立方体 a 的底面长 ...
- DP(DAG) UVA 437 The Tower of Babylon
题目传送门 题意:给出一些砖头的长宽高,砖头能叠在另一块上要求它的长宽都小于下面的转头的长宽,问叠起来最高能有多高 分析:设一个砖头的长宽高为x, y, z,那么想当于多了x, z, y 和y, x, ...
- UVA - 437 The Tower of Babylon(dp-最长递增子序列)
每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...
- UVA 437 The Tower of Babylon巴比伦塔
题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
- UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)
传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...
随机推荐
- SpringBoot非官方教程 | 终章:文章汇总
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-all/ 本文出自方志朋的博客 SpringBo ...
- Spring-boot官方案例分析之log4j
Spring-boot官方案例分析之log4j 运行单元测试分析: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfigur ...
- JavaFXML实现新窗口打开
实现原理顺着往下看就明白了,流程看红色字体.具体还有什么问题可以留言. 主页面配置文件,一共三个按钮.这里说明第一个按钮触发打开新窗口 <?xml version="1.0" ...
- 【Nowcoder 上海五校赛】二数(模拟)
题目描述: 我们把十进制下每一位都是偶数的数字叫做“二数”. 小埃表示自己很聪明,最近他不仅能够从小数到大:2,3,4,5....,也学会了从大数到小:100,99,98...,他想知道从一个数开始数 ...
- 【ppp-chap,pap,mp,mp-group】
PPP链路端口验证(单){ PAP(明文): 主验证方: {local-user user_name:配置本地用户; password {simple||cipher}:配置验证密码; service ...
- 深入理解is_callable和method_exists
一.函数解析 is_callable() 定义: (PHP 4 >= 4.0.6, PHP 5, PHP 7) is_callable — 检测参数是否为合法的可调用结构 bool is_cal ...
- Hadoop(20)-MapReduce框架原理-OutputFormat
1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...
- 阿里云异常网络连接-可疑WebShell通信行为的分析解决办法
2018年10月27日接到新客户网站服务器被上传了webshell脚本木马后门问题的求助,对此我们sine安全公司针对此阿里云提示的安全问题进行了详细分析,ECS服务器被阿里云提示异常网络连接-可疑W ...
- 某CTF收集的Mysql爆表、爆字段语句
Mysql特性 获取数据库名未知函数可爆数据库名 FUNCTION youcanneverfindme17.a does not exist 获取表名and linestring(pro_id) ...
- C语言常用关键语法精华总结
1.关于typedef的用法总结 2.typedef struct的用法 3.typedef函数指针用法 4.数组指针(数组类型的指针)与指针数组 5.真正明白c语言二级指针 6.C语言for循环(及 ...