uva 437 DAG上的动态规划

来源:互联网 发布:pinyinime 源码 编辑:程序博客网 时间:2024/06/15 15:21

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this talehave been forgotten. So now, in line with the educational nature of this contest, we will tell you thewhole story:The babylonians had n types of blocks, and an unlimited supply of blocks of each type.Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block couldbe reoriented so that any two of its three dimensions determined the dimensions of the baseand the other dimension was the height.They wanted to construct the tallest tower possible by stacking blocks. The problem wasthat, in building a tower, one block could only be placed on top of another block as long asthe two base dimensions of the upper block were both strictly smaller than the correspondingbase dimensions of the lower block. This meant, for example, that blocks oriented to haveequal-sized bases couldn’t be stacked.Your job is to write a program that determines the height of the tallest tower the babylonians canbuild with a given set of blocks.InputThe input file will contain one or more test cases. The first line of each test case contains an integer n,representing the number of different blocks in the following data set. The maximum value for n is 30.Each of the next n lines contains three integers representing the values xi, yi and zi.Input is terminated by a value of zero (0) for n.OutputFor each test case, print one line containing the case number (they are numbered sequentially startingfrom 1) and the height of the tallest possible tower in the format‘Case case: maximum height = height’Sample Input110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270Sample OutputCase 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342




#pragma comment(linker, "/STACK:102400000,102400000") #include <algorithm>#include <iostream>#include <iomanip>#include <sstream>#include <cstring>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>#include <queue>#define Pi acos(-1.0)using namespace std;typedef long long ll;int t[33][3], n;int d[33][3];struct node{int x[3];}p[33][3];int check(int i,int ii,int j,int jj){if((p[i][ii].x[0] > p[j][jj].x[0] && p[i][ii].x[1] > p[j][jj].x[1])||(p[i][ii].x[0] > p[j][jj].x[1] && p[i][ii].x[1] > p[j][jj].x[0])){return 1;}return 0; }int dp(int i,int ii){int& ans = d[i][ii];if(ans > 0 ) return ans;ans = p[i][ii].x[2];//ans = 1;for(int j = 0;j < n;j++){for(int jj = 0;jj < 3;jj++){if(check(i,ii,j,jj)) ans = max(ans,dp(j,jj)+p[i][ii].x[2]);//if(check(i,ii,j,jj)) ans = max(ans,dp(j,jj)+ 1);}}return ans;}int main(){int cas = 1;while(scanf("%d", &n), n){memset(d,0,sizeof d);for(int i = 0;i < n;i++){scanf("%d%d%d", &t[i][0], &t[i][1], &t[i][2]);for(int j = 0;j < 3;j++){int b = 0;for(int l = 0;l < 3;l++){if(l == j) continue;p[i][j].x[b] = t[i][l];b++;}p[i][j].x[2] = t[i][j];}}int ans = 0;for(int i = 0;i < n;i++){for(int ii = 0;ii < 3;ii++){ans = max(ans,dp(i,ii));}}printf("Case %d: maximum height = %d\n",cas++, ans);}}


0 0
原创粉丝点击