UVA 437 Tower of Babylon [DP/LIS deformation on DAG]

【链接】:https://cn.vjudge.net/problem/UVA-437

[Question meaning]:
Give you n cubes, let you build them one by one with the length and width as the base (the length and width of the cube below must be greater than the length and width of the above) to find the longest height you can get, the cube can be turned over and over. Swap length, width and height to use. 

【Code】:

#include<bits/stdc++.h>

using namespace std;
const int INF = 1e6;
const int N = 50010;
int n,m,T,c,ca;
struct node
{
    int x, y, z;
}a[N];
int d[N];
bool cmp(node a,node b)
{
    return a.x*a.y < b.x*b.y;
}
void dp()
{
    int Max = 0;
    for(int i=1; i<=c; i++){
        d[i] = a[i].z;
        for(int j=1; j<i; j++){
            if(a[i].x > a[j].x && a[i].y > a[j].y)
                d[i] = max(d[i], d[j] + a[i].z);
        }
        Max = max(Max,d[i]);
    }
    printf("Case %d: maximum height = %d\n", ++ca, Max);
}
intmain ()
{
    while(cin >> n, n)
    {
        int x,y,z;
        c = 0;
        memset(a,0,sizeof(a));
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++){
            cin>>x>>y>>z;
            a[++c]=(node){x,y,z};
            a[++c]=(node){y,x,z};
            a[++c]=(node){z,x,y};
            a[++c]=(node){x,z,y};
            a[++c]=(node){y,z,x};
            a[++c]=(node){z,y,x};
        }
        sort(a+1, a+c+1, cmp);
        dp();
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894083&siteId=291194637