这是按照专题对一些题目的汇总,总体上是按照lrj的《算法艺术与新自学竞赛》的介绍进行的总结 其实就是贴的lrj的题解+部分自己的理解……
DAG上的动态规划A Spy in the Metro UVA - 1025 The Tower of Babylon UVA - 437
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n, T, t[500], m, d, dp[500][500], Case;bool has_train[500][500][2];int main() { while (scanf("%d", &n) != EOF && n) { scanf("%d", &T); for (int i = 1; i < n; ++i) scanf("%d", &t[i]), dp[T][i] = 1 << 30; dp[T][n] = 0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &d); int cnt = 0; for (int j = 1; j <= n; ++j) has_train[d + cnt][j][0] = 1, cnt += t[j]; } scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &d); int cnt = 0; for (int j = n; j >= 1; --j) has_train[d + cnt][j][1] = 1, cnt += t[j - 1]; } for (int i = T - 1; i >= 0; i--) for (int j = 1; j <= n; j++) { dp[i][j] = dp[i + 1][j] + 1; if (j < n && has_train[i][j][0] && i + t[j] <= T) dp[i][j] = min(dp[i][j], dp[i + t[j]][j + 1]); if (j > 1 && has_train[i][j][1] && i + t[j - 1] <= T) dp[i][j] = min(dp[i][j], dp[i + t[j - 1]][j - 1]); } PRintf("Case Number %d: ", ++Case); if (dp[0][1] >= 1 << 30) printf("impossible/n"); else printf("%d/n", dp[0][1]); memset(has_train, 0, sizeof(has_train)); memset(t, 0, sizeof(t)); memset(dp, 0, sizeof(dp)); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n, a[50][3], f[50][3], Case;inline int dp(int x, int y) { int& ans = f[x][y]; if (ans > 0) return ans; ans = 0; int cnt = 0, v1[2], v2[2]; for (int i = 0; i < 3; ++i) if (i != y) v1[cnt++] = a[x][i]; for (int i = 1; i <= n; ++i) for (int j = 0; j < 3; ++j) { cnt = 0; for (int k = 0; k < 3; ++k) if (k != j) v2[cnt++] = a[i][k]; if (v2[0] < v1[0] && v2[1] < v1[1]) ans = max(ans, dp(i, j));//判断是否严格的小于下方的立方体底面的长宽 } ans += a[x][y]; return ans;}int main() { while (scanf("%d", &n) == 1 && n) { for (int i = 1; i <= n; ++i) { scanf("%d %d %d", &a[i][0], &a[i][1], &a[i][2]); sort(a[i], a[i] + 3); } memset(f, 0, sizeof(f)); int res = 0; for (int i = 1; i <= n; ++i) for (int j = 0; j < 3; ++j) res = max(res, dp(i, j)); printf("Case %d: maximum height = %d/n", ++Case, res); memset(a, 0, sizeof(a)); } return 0;}
新闻热点
疑难解答