http://www.rqnoj.cn/Problem_30.html
一、思路
题目是多叉树,原本想
0 0 0 3 =0 1 2 =0 2 1 = 0 3 1 但是要列举的情况太多,就采用转换为二叉树的方法
1 2 3
1、读进多叉树tree[1001][1001]
tree[i][0]来记录本节点的孩子个数
tree[i][j]表示节点i的第j个子节点的编号
2、转化为二叉树binaryTree[1001][1001]
递归生成
先将第一个节点放到左孩子,并生成该节点的根子树
将兄弟节点放到当前生成节点的右孩子处,并生成该节点的根子树
3、记忆化搜索求解
分为三种情况
(1)只取根节点
(2)左孩子取i 右孩子取m-i-1 根节点取1 其中i [0, m-1]
(3)只取右孩子,因为是兄弟节点,所以根节点可以不留人,右孩子取m个人 根节点 和 左孩子都取0个人
取其中最大值
4、问题
第一次写完之后,超时一个点,错误结果一个点
看题解,将cin读取方式改为了scanf,时间缩短了很多
AC了
效率还是很明显的
5、代码如下:
#include <iostream.h>
#include <fstream.h>
int tree[1001][1001], binaryTree[1001][1001], dpTree[1001][1001];
int value[1001];
void ConvertToBinaryTree(int root)
{
int currentRoot, i;
if (!tree[root][0])
{
return;
}
currentRoot = binaryTree[root][1] = tree[root][1];
ConvertToBinaryTree(currentRoot);
for (i=2; i<=tree[root][0]; i++)
{
currentRoot = binaryTree[currentRoot][2] = tree[root][i];
ConvertToBinaryTree(currentRoot);
}
}
void PrintTree(int t[1001][1001], int n)
{
int i, j;
for (i=0; i<=n; i++)
{
for (j=1; j<=t[i][0]; j++)
{
cout<<' '<<t[i][j];
}
cout<<endl;
}
}
void PrintBinaryTree(int root)
{
if (root==0) return;
cout<<root<<endl;
//if (binaryTree[root][1])
{
}
PrintBinaryTree(binaryTree[root][1]);
PrintBinaryTree(binaryTree[root][2]);
}
int DPTree(int root, int m)
{
int i;
if (dpTree[root][m])
{
return dpTree[root][m];
}
if (m==0||root==0)
{
return 0;
}
dpTree[root][m] = value[root];
for (i=0; i<=m-1; i++)
{
if (dpTree[root][m] < DPTree(binaryTree[root][1], i)+value[root]+DPTree(binaryTree[root][2], m-1-i))
{
dpTree[root][m] = dpTree[binaryTree[root][1]][i] + value[root] + dpTree[binaryTree[root][2]][m-1-i];
}
}
if (dpTree[root][m] < DPTree(binaryTree[root][2], m))
{
dpTree[root][m] = dpTree[binaryTree[root][2]][m];
}
return dpTree[root][m];
}
int main()
{
int n, m;
//ifstream inFile("e:\\test.txt");
int i;
//cin>>n>>m;
scanf("%d%d", &n, &m);
//inFile>>n>>m;
//读进财富值
for (i=1; i<=n; i++)
{
//cin>>value[i];
scanf("%d", &value[i]);
//inFile>>value[i];
}
//读进多叉树
for (i=1; i<=n; i++)
{
int from, to;
//cin>>from>>to;
scanf("%d%d", &from, &to);
//inFile>>from>>to;
tree[from][0]++;
tree[from][tree[from][0]] = to;
}
//多叉树转换为二叉树
ConvertToBinaryTree(0);
//打印树
//PrintTree(tree, n);
//PrintBinaryTree(binaryTree[0][1]);
//树形动规最优值
cout<<DPTree(binaryTree[0][1], m);
//inFile.close();
return 0;
}