【算法竞赛入门经典】DAG上的动态规划 例题9-2 UVa437
例题UVa437
Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole 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 could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.
They wanted to construct the tallest tower possible by stacking blocks. The problem was that, in building a tower, one block could only be placed on top of another block as long as thetwobasedimensionsoftheupperblockwerebothstrictlysmallerthanthecorresponding base dimensions of the lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn’t be stacked.
Your job is to write a program that determines the height of the tallest tower the babylonians can build with a given set of blocks.
Input
The 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.
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format
‘Case case: maximum height = height’
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
分析
当前的顶面状态会对是否能安放某一个立方体产生影响,进而影响到高度。
鉴于顶面状态是输入的确定的几个量,没有必要用dp[a][b]存储全部宽度,而且在a,b最大值很大的时候就不合适了。因此,可以采用替代的方法,利用三个vector分别记录a,b,c。为了方便后续比较,a,b,c按照大小从小到大存入。这样,dp[i][j]中的i就代表了第i的立方体的第j种情况的时候,其上方堆叠的所有立方体有多高。j只有三种可能性。这样就减少了储存开销。(上方!上方!上方!重要的东西说三遍)
对于每一种状态d[i][j]来说,取的它的a和b,再遍历所有立方体的所有摆放情况ai,bi;鉴于之前已经对a,b,c按顺序存储了,所有只需要比较ai与a、bi与b是否满足严格小于关系即可。
则d[i][j]=for(……) for(……) d[i][j]=max(d[i][j],d[di][dj])
样例实现代码
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#define maxn 30+5
using namespace std;
vector<int>a;
vector<int>b;
vector<int>c;
int dp[maxn][3], temp[3];
int n;
void get_a(int i, int num, int *v) {
if (num == 0) {
v[0] = a[i];
v[1] = b[i];
}
else if (num == 1) {
v[0] = a[i];
v[1] = c[i];
}
else {
v[0] = b[i];
v[1] = c[i];
}
}
int geth(int i, int num) {
if (num == 0)
return c[i];
else if (num == 1)
return b[i];
else
return a[i];
}
int fundp(int i, int j) {
int &ans = dp[i][j];
if (ans>0)
return ans;
int v1[2], v2[2];
get_a(i, j, v1);
for (int di = 0; di<n; di++) {
for (int dj = 0; dj<3; dj++) {
get_a(di, dj, v2);
if (v1[0]>v2[0] && v1[1]>v2[1])
ans = max(ans, fundp(di, dj));
}
}
ans += geth(i, j);
return ans;
}
int main() {
int kase = 0;
while (cin >> n && n) {
a.clear();
b.clear();
c.clear();
for (int i = 0; i<n; i++) {
cin >> temp[0] >> temp[1] >> temp[2];
sort(temp, temp + 3);
a.push_back(temp[0]);
b.push_back(temp[1]);
c.push_back(temp[2]);
}
memset(dp, 0, sizeof(dp));
int ans = 0;
for (int i = 0; i<n; i++)
for (int j = 0; j<3; j++)
ans = max(ans, fundp(i, j));
cout << "Case " << ++kase << ": maximum height = " << ans << endl;
}
return 0;
}