bzoj5290: [Hnoi2018] Road [tree dp/memory search]

portal

Problem solving ideas:

When I took the test, I thought it was a fairy question, so I started to play dfs violence, and then I suddenly found that it could be memorized, and then there was no more... Isn't this popular group dp?
f [ i ] [ x ] [ and ] means with i is the rooted subtree with x road, and The optimal value of the unrenovated railway can be searched by memorization.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}
const int N=40005;
int n,l[N],r[N],a[N],b[N],c[N];
ll f[N/2][41][41];
ll dfs(int u,int x,int y)
{
    if(u>=n)return 1ll*(a[u]+x)*(b[u]+y)*c[u];
    if(f[u][x][y])return f[u][x][y];
    return f[u][x][y]=min(dfs(l[u],x+1,y)+dfs(r[u],x,y),dfs(l[u],x,y)+dfs(r[u],x,y+1));
}
int main()
{
    //freopen("road.in","r",stdin);
    //freopen("road.out","w",stdout);
    n=getint();
    for(int i=1,x;i<n;i++)
    {
        x=getint();
        l[i]=x>0?x:n-1-x;
        x=getint();
        r[i]=x>0?x:n-1-x;
    }
    for(int i=n;i<2*n;i++)a[i]=getint(),b[i]=getint(),c[i]=getint();
    cout<<dfs(1,0,0)<<'\n';
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326130998&siteId=291194637