uva437-The Tower of Babylon

题目链接请戳 这里

解题思路

带权DAG上的最大权和路。

可以先对每个木块的三个维度排序方便后续处理。

(思路来自紫书)

dp[i][j]表示以第i个木块为底,j=0/1/2时以长/宽/高 为高,时能组成的最高高度。

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 40
using namespace std;

int blo[N][3];
int dp[N][3];
int n;

int d(int x, int y)
{
  int &ans = dp[x][y];
  int u, v;
  if (ans > 0) return ans;
  //y决定谁为高
  if (y == 0) { ans = blo[x][0]; u = blo[x][1]; v = blo[x][2]; }
  else if (y == 1) { ans = blo[x][1]; u = blo[x][0]; v = blo[x][2]; }
  else { ans = blo[x][2]; u = blo[x][0]; v = blo[x][1]; }
  //因为事先对维度排序,可以减少判断
  for (int j = 1; j <= n; j++)
    for (int k = 0; k <= 2; k++) {
      if (k == 0 && blo[j][1] < u && blo[j][2] <v)
    ans = max(ans, d(j, k) + blo[x][y]);
      else if (k == 1 && blo[j][0] <u && blo[j][2] <v)
    ans = max(ans, d(j, k) + blo[x][y]);
      else if (k == 2 && blo[j][0] <u && blo[j][1] <v)
    ans = max(ans, d(j, k) + blo[x][y]);
    }
  return ans;
}

int main()
{
  int t = 0;
  while (scanf("%d", &n) != EOF && n) {
    memset(dp, 0, sizeof(dp));
    //现对三个维度排序
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &blo[i][0], &blo[i][1], &blo[i][2]);
      sort(blo[i], blo[i] + 3);
    }
    for (int i = 1; i <= n; i++)
      for (int k = 0; k <= 2; k++) dp[i][k] = d(i, k);
    int maxn = 0;
    for (int i = 1; i <= n; i++)
      for (int k = 0; k <= 2; k++)
    maxn = max(maxn, dp[i][k]);
    printf("Case %d: maximum height = ", ++t);
    printf("%d\n", maxn);
  }
  return 0;
}
时间: 2024-12-29 13:41:34

uva437-The Tower of Babylon的相关文章

uva437 - The Tower of Babylon(DAG上的DP)

题目:uva437 - The Tower of Babylon(DAG上的DP) 题目大意:给你一些立方体,给出长宽高XYZ.现在希望你将这些立方题叠起来,使得最后的高度最大,并且这些立方体是可以无限次使用的,但是一个立方体要在另一个立方体的上面的话是需要满足这个立方体的底面是可以完全包含在下面的那个立方体的底面. 解题思路:其实这里的无限次使用没有什么用,因为一个立方体最多使用三次就不可能是再用.输入的一个立方体其实可以变成三个确定长宽高的立体.然后将这些立方体先做预处理,如果立方体j能够放

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 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

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

UVA The Tower of Babylon

The Tower of Babylon 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

POJ 2241 The Tower of Babylon

The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 224164-bit integer IO format: %lld      Java class name: Main Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d

UVA 437 十九 The Tower of Babylon

The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 437 Appoint description:  System Crawler  (2015-08-29) Description Perhaps you have heard of the legend of the Tower of Babylon. No

POJ2241——The Tower of Babylon

The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1244 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

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

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

poj2241 The Tower of Babylon

The Tower of Babylon 题意:给你n种石头,长x,宽y,高z,每种石头数目无限,一块石头能放到另一块上的条件是:长和宽严格小于下面的石头.问叠起来的最大高度. /* 有些类似“叠箱子”问题,看起来一种砖有无限多个,其实最多只能用到两次. 说下我的思路吧,一块砖有3个数据,虽然3!=6,但本质上还是只有3种,把这三种都表示出来,使x<=y:这样就有了3n组数据.因为我不会建图,就把这3n组数据再排列一下,使一块砖只能放到它后面的砖之上,而绝不能放到之前的砖上,即按x为一级y为二级