Codevs 1090 plus two fork tree

Source: Internet
Author: User

A tree DP, the equation is in the program, pay attention to the sequence and initialization of the loop.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace Std;
Long Long dp[35][35],n,w[35],c[35][35];
void find (Long long left,long long right)
{
if (left>right) return;
printf ("%d", c[left][right]);
if (left!=right)
{
Find (LEFT,C[LEFT][RIGHT]-1);
Find (C[left][right]+1,right);
}
Return
}
int main ()
{
Memset (Dp,0,sizeof (DP));
Memset (C,0,sizeof (c));
scanf ("%lld", &n);
For (Long long i=1;i<=n;i++)
scanf ("%lld", &w[i]);
For (Long long i=1;i<=n;i++)
{
Dp[i][i]=w[i];
Dp[i][i-1]=1;
C[i][i]=i;
}
For (Long long j=1;j<=n;j++)
For (Long long i=j-1;i>=1;i--)
for (int k=i;k<=j;k++)
{
if (Dp[i][j]<dp[i][k-1]*dp[k+1][j]+w[k])
{
DP[I][J]=DP[I][K-1]*DP[K+1][J]+W[K];
C[i][j]=k;
}
}
printf ("%lld\n", Dp[1][n]);
Find (1,n);
return 0;
}

Codevs 1090 plus two fork tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.