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 ...
随机推荐
- Question20171231 聊聊为什么32bit只能支持4GB内存
1. 32位系统最大只能支持4GB内存之由来 也许大家对这个问题都不陌生,实际装过系统用过电脑的朋友可能都有这样的经历:自己电脑配的是4G的内存条,可是装完系统之后发现电脑上显示的只有3.2G左右可用 ...
- [国家集训队]小Z的袜子(莫队,概率)
题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编 ...
- 思维导图 XMind 8 Update 8 Pro for Mac 中文破解版
破解包下载地址 在官网下载8版本就可以了
- 爬虫——使用BeautifulSoup4的爬虫
我们以腾讯社招页面来做示例:http://hr.tencent.com/position.php?&start=0#a 如上图,使用BeautifulSoup4解析器,将图1中229页,每页1 ...
- 使用DOM对表格进行增删
---恢复内容开始--- 声明本文旨在练习dom 其中可以链接数据 或者使用ajax 实现的我全用的dom因为我在学dom. 一. 表格构建 <section id="section_ ...
- js分割字符串
js分割字符串 我想达到通过 : 分割 只要第一次分割,后面的内容不使用分割 不行,没找到可以直接用的方法,不过可以通过其它方式达到效果 eg: str.split(':',2)[0] (第一个分隔符 ...
- hash和history的区别
vue-router 中hash模式和history模式. 在vue的路由配置中有mode选项,最直观的区别就是在url中hash 带了一个很丑的 # ,而history是没有#的.vue默认使用ha ...
- C# WebClient类上传和下载文件
这篇文章主要介绍了C# WebClient类用法实例,本文讲解使用WebClient下载文件.OpenWriter打开一个流使用指定的方法将数据写入到uri以及上传文件示例,需要的朋友可以参考下 ...
- Nodejs 使用 SerialPort 调用串口
工作经常使用串口读写数据,electron 想要替代原来的客户端,串口成了必须要突破的障碍. get --> https://github.com/EmergingTechnologyAdvi ...
- No configuration found for this host:al
想尝试多个agent来进行传输数据其中的一个配置文件如下: al.sources = r1al.sinks = k1al.channels = c1 al.sources.r1.type = avro ...