Uva437-The Tower of Babylon (DP on DAG)

Source: Internet
Author: User

Question: uva437-The Tower of Babylon (DP on DAG)


Here are some cubes for you and XYZ for length, width, and height. Now, I hope you can fold these cubes to maximize the final height, and these cubes can be used infinitely, but if a cube needs to be on the top of another cube, the bottom of the cube must be completely contained in the bottom of the cube.


Solution: in fact, unlimited use is useless here, because a cube can be used up to three times and cannot be reused. The input cube can actually be changed to three dimensions that determine the length, width, and height. Then, pre-process these cubes first. If cube J can be placed on cube I, then I to J has a directed edge. In this way, you only need to search for this directed acyclic graph. DP [I] [J] indicates the maximum height from the I cube to the J cube (if possible, the I is on the J cube. DP [I] [J] = max (DP [J] [k] + V [I ]) k> = 1 & K <= 3 * N & G [J] [k] = 1 Initial Value DP [J] [k] = V [J ]; here we use the memory-based search, and we feel that the recurrence sequence is not clear.


Code:

#include <cstdio>#include <cstring>const int N = 95;typedef long long ll;int n;int G[N][N];ll dp[N][N];struct BLOCK { int x, y, z;}b[N];bool judge (int i, int j) {if (b[i].x > b[j].x && b[i].y > b[j].y)return true;if (b[i].y > b[j].x && b[i].x > b[j].y)return true;return false;}void handle () {memset (G, 0, sizeof (G));for (int i = 0; i < 3 * n; i++)for (int j = 0; j < 3 * n; j++) {if (i != j && judge(i, j))G[i][j] = 1;}}ll Max (const ll a, const ll b) { return a > b? a: b; }int DP (int x1, int y1) {ll& ans = dp[x1][y1];if (ans != -1)return ans;for (int i = 0; i < 3 * n; i++) {if (y1 == i)continue;if (G[y1][i])ans = Max (ans, DP(y1, i) + b[x1].z);}if (ans == -1)ans = b[y1].z + b[x1].z;return ans;}int main () {int x, y, z;int cas = 0;ll ans;while (scanf ("%d", &n), n) {for (int i = 0; i < 3 * n; i = i + 3) {scanf ("%d%d%d", &b[i].x, &b[i].y, &b[i].z);b[i + 1].z = b[i].x;b[i + 1].y = b[i].y;b[i + 1].x = b[i].z;b[i + 2].z = b[i].y;b[i + 2].x = b[i].x;b[i + 2].y = b[i].z;}handle();ans = 0;memset (dp, -1, sizeof (dp));for (int i = 0; i < 3 * n; i++)for (int j = 0; j < 3 * n; j++)if (G[i][j])ans = Max (ans, DP(i, j));printf ("Case %d: maximum height = %lld\n", ++cas, ans);}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.