UVa 437 the Tower of Babylon:dp&dag

Source: Internet
Author: User
Tags cas time limit

437-the Tower of Babylon

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=378

Ideas:

For a (x,y,z) brick, it can be placed in 3 positions: (top two for ground, rear one for high)

X, Y, Z

X, Z, y

Y, Z, x

Record each posture and become a brick with a 3*n fixed posture.

Then build the map and find the longest road on the DAG.

Complete code:

/*0.015s*/#include <bits/stdc++.h> using namespace std;  
    
int a[100][3], dp[100], N;  
    int DAG (int x) {if (dp[x]!=-1) return dp[x];  
        DP[X] = a[x][2];///high for (int y = 0; y < n; ++y) {if (y = = x) continue; if (A[y][0] < a[x][0] && a[y][1] < a[x][1] && Dp[x] < DAG (y) + a[x][2]) dp[x] = DAG  
    (y) + a[x][2];  
return dp[x];  
    int main () {int cas = 0, maxh, I;  
        while (scanf ("%d", &n), n) {n + = n << 1;  
        Memset (DP,-1, sizeof (DP));  
            for (i = 0; i < n; i + + 3) {scanf ("%d%d%d", &a[i][0], &a[i][1], &a[i][2));  
            Sort (A[i], A[i] + 3);  
            A[i + 1][0] = a[i][0], a[i + 1][1] = a[i][2], a[i + 1][2] = a[i][1];  
        A[i + 2][0] = a[i][1], a[i + 2][1] = a[i][2], a[i + 2][2] = a[i][0];  
        } maxh = 0; for (i = 0; i < n;  
        ++i) if (Maxh < DAG (i)) maxh = Dp[i];  
    printf ("Case%d:maximum height =%d\n", ++cas, Maxh);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.