题解
读题是做题关键
我们设(dp[u][l][r])表示(u)节点上方没改(l)条公路和(r)条铁路
然后记忆化搜索,枚举这条点改左边还是右边
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('
')
#define space putchar(' ')
#define MAXN 20005
//#define ivorysi
using namespace std;
typedef long long int64;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int ls[MAXN],rs[MAXN];
int64 a[MAXN],b[MAXN],c[MAXN];
int64 dp[MAXN][45][45];
int64 dfs(int u,int l,int r) {
if(u >= N) {
int t = u - N + 1;
return c[t] * (a[t] + l) * (b[t] + r);
}
if(dp[u][l][r] != -1) return dp[u][l][r];
dp[u][l][r] = min(dfs(ls[u],l,r) + dfs(rs[u],l,r + 1),dfs(ls[u],l + 1,r) + dfs(rs[u],l,r));
return dp[u][l][r];
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);
for(int i = 1 ; i < N ; ++i) {
read(ls[i]);read(rs[i]);
if(ls[i] < 0) ls[i] = -ls[i] + N - 1;
if(rs[i] < 0) rs[i] = -rs[i] + N - 1;
}
memset(dp,-1,sizeof(dp));
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);read(b[i]);read(c[i]);
}
out(dfs(1,0,0));enter;
}