/*UVa 437 - The Tower of Babylon
* 矩形嵌套问题 DP + DAG + LIS
* */
import java.util.Arrays;
import java.util.Scanner;
class Block {
int x;
int y;
int z;
public Block(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
class Main {
static final int MAXN = 91;
static int n;
static int[][] g = new int[MAXN][MAXN];
static Block[] b = new Block[MAXN];
static int[] d = new int[MAXN];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cn = 0;
while ((n = scanner.nextInt()) > 0) {
cn++;
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
b[3 * i] = new Block(x, y, z);
b[3 * i + 1] = new Block(x, z, y);
b[3 * i + 2] = new Block(y, z, x);
}
n *= 3;
Arrays.fill(d, -1);
for (int i = 0; i < n; i++) {
Arrays.fill(g[i], 0);
}
build_graph();
int max = 0;
for (int j = 0; j < n; j++) {
int t = dp(j);
max = Math.max(t, max);
}
System.out.println("Case " + cn + ": maximum height = " + max);
}
}
private static void build_graph() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
continue;
if (in(i, j))
g[i][j] = 1;
else if (in(j, i))
g[j][i] = 1;
}
}
}
private static boolean in(int i, int j) {
Block e = b[i];
Block f = b[j];
if ((e.x < f.x && e.y < f.y) || (e.x < f.y && e.y < f.x))
return true;
return false;
}
private static int dp(int i) {
if (d[i] != -1)
return d[i];
int temp = 0;
for (int j = 0; j < n; j++) {
if (g[i][j] == 1) {
int t = dp(j);
if (temp < t)
temp = t;
}
}
return d[i] = temp + b[i].z;
}
}
UVa 437 - The Tower of Babylon
最新推荐文章于 2019-06-28 10:21:47 发布