DP (DAG) UVA 437 the Tower of Babylon

Source: Internet
Author: User

Topic Portal

Test instructions: Give some of the length and width of the bricks, bricks can be stacked on the other side of the long width is smaller than the length of the following head, ask how high can be stacked up

Analysis: Set a brick of the length of the width of x, Y, Z, then want to be more than X, Z, y and y, X, Z of the bricks, if I can be stacked on J, then g[i][j] = True, converted to dag problem, Dp[i] indicates that block I stack at the highest height of the upper

Harvesting: Transforming into a classic model

Code:

/************************************************* author:running_time* Created time:2015-8-28 18:00:01* File Na Me:UVA_437.cpp ************************************************/#include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e2 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;struct block{int x, Y, Z;}    B[n];bool g[n][n];int dp[n];int n;int DFS (int u) {if (Dp[u]! =-1) return dp[u]; Dp[u] = b[u].z;for (int i=1; i<=n; ++i) {if (G[u][i]) {Dp[u] = max (Dp[u], DFS (i) + b[u].z);}} return dp[u];} BOOL Check (int I, Int j) {if (b[i].x < b[j].x && B[i].y < B[J].Y) return True;if (b[i].x < b[j].y && B[i].y < b[j].x) return True;return false;} int main (void) {int cas = 0;while (scanf ("%d", &n) = = 1) {if (n = = 0) break;for (int i=1; i<=n; ++i) {scanf (" %d%d%d ", &b[i].x, &b[i].y, &b[i].z); b[n+i].x = b[i].x, b[n+i].y = b[i].z, b[n+i].z = b[i].y;b[2*n+i].x = B[i]. Y, b[2*n+i].y = b[i].z, b[2*n+i].z = b[i].x;} Memset (g, false, sizeof (g)), n *= 3;for (int i=1; i<=n; ++i) {for (int j=i+1; j<=n; ++j) {if (check (i, j)) G[i][j] = True;if (Check (J, i)) g[j][i] = true;}} Memset (DP,-1, sizeof (DP)), int ans = 0;for (int i=1; i<=n; ++i) {ans = max (ans, DFS (i));}    printf ("Case%d:maximum height =%d\n", ++cas, ans);} return 0;}

  

DP (DAG) UVA 437 the Tower of Babylon

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.