[bzoj5290][记忆化搜索]道路

17 篇文章 0 订阅

不想搞题面所以传送门
题解

记忆化搜索的复杂度真的玄学2333
f[i][j][k]表示第i个城市上面有j个铁路k个公路
瞎搜索

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
inline int read()
{
    int f=1,x=0;char ch=getchar();
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline LL readx()
{
    LL f=1,x=0;char ch=getchar();
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
LL f[21000][45][45],ans;
int s[21000],t[21000];
LL C[21000],A[21000],B[21000];
LL dfs(int x,int u,int v)//公 铁 
{
    if(x<0)return C[abs(x)]*(A[abs(x)]+u)*(B[abs(x)]+v);
    if(f[x][u][v]!=-1)return f[x][u][v];
    LL cnt=0;
    cnt+=dfs(s[x],u,v);//翻修公路 
    cnt+=dfs(t[x],u,v+1);
    f[x][u][v]=cnt;cnt=0;
    cnt+=dfs(t[x],u,v);
    cnt+=dfs(s[x],u+1,v);
    f[x][u][v]=min(f[x][u][v],cnt);
    return f[x][u][v];
}
int n;
int main()
{
    //freopen("road.in","r",stdin);
//  freopen("road.out","w",stdout);
    n=read();
    for(int i=1;i<n;i++)s[i]=read(),t[i]=read();
    for(int i=1;i<=n;i++)A[i]=readx(),B[i]=readx(),C[i]=readx();
    memset(f,-1,sizeof(f));
    dfs(1,0,0);
    printf("%lld\n",f[1][0][0]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值