最优二叉树II
最优二叉树II
https://www.nowcoder.com/questionTerminal/0d939e874a004f449a370aca1346dd5c?answerType=1&f=discussion
using namespace std;
const int maxn = 310;
const int inf = 1<<30;
vector<int>v(maxn,0);
//备忘录
int vis[maxn][maxn][maxn];
//以father为根节点能够得到的最优树
int dp(int l,int r, int father){
if(l>r) return 0;
if(vis[l][r][father]!=-1) return vis[l][r][father];
int ret = inf;
// 这些节点中每个均可能做根 需要遍历一下
for(int i=l;i<=r;++i){
//递归计算左右子树的最小代价
int left = dp(l,i-1,i);
int right = dp(i+1,r,i);
// 总最小=min(左子树+右子树+选出的根节点值*father节点值)
ret = min(ret,left+right+v[i]*v[father]);
}
//备忘录记下已经取得的值
return vis[l][r][father]=ret;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i) cin>>v[i];
memset(vis,-1,sizeof(vis));
int ans = dp(1,n,0);
printf("%d\n",ans);
return 0;
}