UVa 437 - The Tower of Babylon

/*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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值