POJ 2241The Tower of Babylon(DAG DP)

/* 解题报考:DAG上dp 1、dp(n)表示以n为底但不包括n为底时的最大高度(因为有地面), 假设第i个block可以放到n上,则dp(n) = max(dp(i) + a[i]) a[i]代表第i块的高度 注意这里i是任意的 2、本题不用建图,可以放到上面可以想象成一条有向边 */ #include <iostream> #include <cstring> using namespace std; const int MAX = 9999999; int anBlock[100][3]; int height[100]; int n; int nNumber;//实际block的块数 void InitBlock(int nBlock, int a, int b, int c) { anBlock[nBlock][0] = a; anBlock[nBlock][1] = b; anBlock[nBlock][2] = c; } int dp(int nWhich) { if(height[nWhich] != -1) return height[nWhich]; if(nWhich > nNumber) return 0; int nTemp = 0; for(int i=1; i<=nNumber; i++) { if((anBlock[nWhich][0] > anBlock[i][0] && anBlock[nWhich][1] > anBlock[i][1]) || (anBlock[nWhich][1] > anBlock[i][0] && anBlock[nWhich][0] > anBlock[i][1])) nTemp = dp(i) + anBlock[i][2]; if(nTemp > height[nWhich]) height[nWhich] = nTemp; } return height[nWhich]; } int main() { int a, b, c; int i; int nCase = 0; while(cin>>n && n) { memset(height, -1, sizeof(height)); anBlock[0][0] = anBlock[0][1] = anBlock[0][2] = MAX; nNumber = 0; for(i=0; i<n; i++) { cin>>a>>b>>c; InitBlock(++nNumber, a, b, c); InitBlock(++nNumber, a, c, b); InitBlock(++nNumber, c, b, a); } cout<<"Case "<<++nCase<<": "<<"maximum height = "<<dp(0)<<endl; } return 0; }

你可能感兴趣的:(POJ 2241The Tower of Babylon(DAG DP))