# [HNOI2018]道路

## 题意

${c}_{i}\left({a}_{i}+x\right)\left({b}_{i}+y\right)$

## 题解

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

$f\left[u\right]\left[i\right]\left[j\right]={c}_{u}\left({a}_{u}+i\right)\left({b}_{u}+j\right)$

$f\left[u\right]\left[i\right]\left[j\right]=min\left\{f\left[lson\right]\left[i+1\right]\left[j\right]+f\left[rson\right]\left[i\right]\left[j\right],f\left[lson\right]\left[i\right]\left[j\right]+f\left[rson\right]\left[i\right]\left[j+1\right]\right\}$

$Ans=f\left[1\right]\left[0\right]\left[0\right]$

#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;
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$$lajiDP$送我退役