The Tower of Babylon UVA

一道dp

#include<bits/stdc++.h>
using namespace std;

const int maxn = 35;

struct node {
    int x,y,z;
    node() {}
    node(int x,int y,int z) :x(x),y(y),z(z) {}
    bool operator < (const node &n)const{
        return (x < n.x&& y < n.y) || (x < n.y&& y < n.x); 
    }
}lft[maxn*3];

int f[maxn*3],m,Case=0,n;

int dp() {
    int ans=0;
    for(int i=1;i<=3*n;i++) {
        f[i]=lft[i].z;
        for(int j=1;j<i;j++) {
                   if(lft[j]<lft[i]) f[i]=max(f[i],f[j]+lft[i].z);
        }
        ans=max(ans,f[i]);
    }
    return ans;
}

bool cmp(node a,node b) {
    return a.x*a.y<b.x*b.y;
}

int main() {
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)&&n) {
        memset(f,0,sizeof(f));m=0;
        for(int i=1;i<=n;i++) {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            lft[++m]=node(x,y,z);
            lft[++m]=node(x,z,y);
            lft[++m]=node(z,y,x);
        }
        sort(lft+1,lft+1+m,cmp);
         printf("Case %d: maximum height = %d
",++Case,dp());
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/plysc/p/10842657.html