[HNOI2018]道路

题意

给你一颗二叉树 , , 每个叶子节点i有三个属性 ai,bi,ci a i , b i , c i

每个非叶子节点都能标记往左右儿子的边中的一条边 ( ( 分别记为L边和 R R )

设叶子节点 i i 到根的路径上没有被标记的L边有 x x ,R边有 y y

那么i的贡献就是

ci(ai+x)(bi+y) c i ( a i + x ) ( b i + y )

最小化所有点的贡献和


题解

那个式子是唬你的

直接考虑倒推

f[u][i][j] f [ u ] [ i ] [ j ] 表示从根到 u u i条没标记的 L L 边和j条没标记的 R R

对于每个叶子节点枚举有多少没有被标记的L边和 R R

f[u][i][j]=cu(au+i)(bu+j)

对于非叶子节点枚举删哪条边

f[u][i][j]=min{f[lson][i+1][j]+f[rson][i][j],f[lson][i][j]+f[rson][i][j+1]} f [ u ] [ i ] [ j ] = m i n { f [ l s o n ] [ i + 1 ] [ j ] + f [ r s o n ] [ i ] [ j ] , f [ l s o n ] [ i ] [ j ] + f [ r s o n ] [ i ] [ j + 1 ] }

Ans=f[1][0][0] A n s = f [ 1 ] [ 0 ] [ 0 ]

貌似这题还可以卡空间

#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(register int i=fi[u],v=e[i].to;i;v=e[i=e[i].nx].to)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char ss[1<<17],*A=ss,*B=ss;
inline char gc(){return A==B&&(B=(A=ss)+fread(ss,1,1<<17,stdin),A==B)?-1:*A++;}
template<class T>inline void sd(T&x){
    char c;T y=1;while(c=gc(),(c<48||57<c)&&c!=-1)if(c==45)y=-1;x=c-48;
    while(c=gc(),47<c&&c<58)x=x*10+c-48;x*=y;
}
const int N=4e4+1;
typedef int arr[N];
typedef long long ll;
int n,m,Top,Tot;arr a,b,c,s,t,Num,S;ll f[82][41][41];
void dfs(int u,int x,int y){
    int p=Num[u]=Top?S[Top--]:++Tot;
    if(!s[u]){
        fp(i,0,x)fp(j,0,y)f[p][i][j]=(ll)c[u]*(a[u]+i)*(b[u]+j);
        return;
    }
    dfs(s[u],x+1,y),dfs(t[u],x,y+1);
    int ls=Num[s[u]],rs=Num[t[u]];
    fp(i,0,x)fp(j,0,y)
        f[p][i][j]=min(f[ls][i+1][j]+f[rs][i][j],f[ls][i][j]+f[rs][i][j+1]);
    S[++Top]=ls,S[++Top]=rs;
}
int main(){
    #ifndef ONLINE_JUDGE
        file("s");
    #endif
    sd(n);m=2*n-1;int u,v;
    fp(i,1,n-1)sd(u),sd(v),s[i]=u<0?n-1-u:u,t[i]=v<0?n-1-v:v;
    fp(i,n,m)sd(a[i]),sd(b[i]),sd(c[i]);
    dfs(1,0,0);
    printf("%lld\n",f[Num[1]][0][0]);
return 0;
}

lajiDP l a j i D P 送我退役

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值