标签:style blog class code tar color
#include <stdio.h> #include <string.h> #define max(a,b) ((a)>(b)?(a):(b)) const int N = (1<<20) + 5; const int M = 25; int h, k, node[N], dp[N][M], vis[N][M]; void build(int u, int d) { memset(vis[u], 0, sizeof(vis[u])); if (d == h) return; scanf("%d", &node[u]); build(u * 2 + 1, d + 1); build(u * 2 + 2, d + 1); } void dfs(int u, int k, int d) { if (vis[u][k]) return; vis[u][k] = 1; dp[u][k] = node[u]; if (d + 1 == h) return; for (int i = 1; i < k; i++) { int l = u * 2 + 1; int r = u * 2 + 2; dfs(l, i, d + 1); dfs(r, k - i, d + 1); dp[u][k] = max(dp[u][k], dp[l][i] + dp[r][k - i]); } } int main() { while (~scanf("%d", &h) && h != -1) { h++; scanf("%d", &k); build(0, 0); dfs(0, k, 0); printf("%d\n", dp[0][k]); } return 0; }
11782 - Optimal Cut(树形DP+记忆化搜索),布布扣,bubuko.com
11782 - Optimal Cut(树形DP+记忆化搜索)
标签:style blog class code tar color
原文地址:http://blog.csdn.net/accelerator_/article/details/24837611