題意 "題目鏈接" Sol ~~每當出題人想起他出的HNOI 2018 Day2T3,他都會激動的拍打著輪椅~~ 讀題比做題用時長系列。。。 $f[i][a][b]$表示從根到$i$的路徑上,有$a$條公路未被翻修,$b$條鐵路未被翻修 然後xjb轉移一下 比較好奇為啥不會MLE.. cpp inc ...
題意
Sol
每當出題人想起他出的HNOI 2018 Day2T3,他都會激動的拍打著輪椅
讀題比做題用時長系列。。。
\(f[i][a][b]\)表示從根到\(i\)的路徑上,有\(a\)條公路未被翻修,\(b\)條鐵路未被翻修
然後xjb轉移一下
比較好奇為啥不會MLE..
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e5 + 10;
const LL INF = 1e18;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while (c < '0' || c > '9') {if (c == '-')f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x * f;
}
int N, ls[MAXN], rs[MAXN];
LL A[MAXN], B[MAXN], C[MAXN];
vector<int> v[MAXN];
LL f[20001][41][41];
LL dfs(int x, LL a, LL b) {
if(x > N) return C[x] * (A[x] + a) * (B[x] + b);
if(f[x][a][b] <= INF) return f[x][a][b];
else f[x][a][b] = min(dfs(ls[x], a, b) + dfs(rs[x], a, b + 1), dfs(ls[x], a + 1, b) + dfs(rs[x], a, b));
return f[x][a][b];
}
int main() {
// freopen("a.in", "r", stdin);
memset(f, 0x7f, sizeof(f));
N = read();
for(int i = 1; i <= N - 1; i++) {
ls[i] = read(); rs[i] = read();
if(ls[i] < 0) ls[i] = -ls[i] + N;
if(rs[i] < 0) rs[i] = -rs[i] + N;
}
for(int i = N + 1; i <= 2 * N; i++) A[i] = read(), B[i] = read(), C[i] = read();
if(N == 1) cout << 0 << endl;
else cout << dfs(1, 0, 0);
return 0;
}