Uva 437-The Tower of Babylon(DP)

题目链接:点击打开链接

DAG上最长路、

题意:给出n种长方体,(每种无限),要求能摞的最大高度。连边是大的连小的。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 10000002
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 10000007
#define pp pair<int,int>
#define ull unsigned long long
using namespace std;
int n,m,dp[110],x[110],y[110],z[110];
bool ma[110][110];
void input()
{
    m=n*3;
    for(int i=1;i<=m-2;i+=3){
        scanf("%d %d %d",x+i,y+i,z+i);
        x[i+1]=y[i];y[i+1]=z[i];z[i+1]=x[i];
        x[i+2]=y[i+1];y[i+2]=z[i+1];z[i+2]=x[i+1];
    }
}
void build()
{
    memset(ma,0,sizeof(ma));
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=m;i++)
    for(int j=i+1;j<=m;j++){
        if((x[i]>x[j]&&y[i]>y[j])||(x[i]>y[j]&&y[i]>x[j]))
            ma[i][j]=1;
        else if((x[j]>x[i]&&y[j]>y[i])||(x[j]>y[i]&&y[j]>x[i]))
            ma[j][i]=1;
    }
}
int dfs(int x)
{
    int& ans=dp[x];
    if(ans)return ans;
    ans=z[x];
    for(int i=1;i<=m;i++){
        if(ma[x][i])ans=max(ans,dfs(i)+z[x]);
    }
    return ans;
}
int main()
{
    int cas=1;
	while(scanf("%d",&n)&&n){
        input();
        build();
        int ans=-INF;
        for(int i=1;i<=m;i++)
            ans=max(ans,dfs(i));
        printf("Case %d: maximum height = %d\n",cas++,ans);
	}
	return 0;
}
时间: 2024-10-10 15:16:40

Uva 437-The Tower of Babylon(DP)的相关文章

UVa 437 The Tower of Babylon(动态规划)

传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n

uva 437 The Tower of Babylon(动态规划 嵌套矩形问题最长路)

有思路就去做,要相信自己 多处理更复杂的情况,你就不觉得现在复杂了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; struct soli { ll a,b,c; }s[40]; int n; ll d[40][3]; int vis[40][3]; ll answer[40][3]; ll dp(int s1,int s2

UVA - 437 The Tower of Babylon(dp-最长递增子序列)

每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 1818 struct Node { int x,y,z,area; void init(int a,

UVA 437 The Tower of Babylon DP

有向图DAG The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So n

UVa 437 The Tower of Babylon(DP 最长条件子序列)

 题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于另一个时  这个长方体可以放在另一个上面  要求输出这样累积起来的最大高度 因为每个长方体都有3种放法  比较不好控制   可以把一个长宽高分成三个长方体  高度是固定的  这样就比较好控制了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 105 int x[max

UVa 437 The Tower of Babylon

Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n typ

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

UVA 437 The Tower of Babylon巴比伦塔

题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http://acm.hust.edu.cn/vjudge/problem/19214 AC代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 50010 i

uva 10285 The Tower of Babylon(记忆化搜索)

Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB Michael likes snowboarding. That's not very surprising, since snowboarding is really great. The bad thing is that in order to