题目传送门
Solution
观察这个
ci×(ai+x)×(bi+y)
c
i
×
(
a
i
+
x
)
×
(
b
i
+
y
)
这个式子,
想到状态定义:
f[u][i][j]
f
[
u
]
[
i
]
[
j
]
表示
u
u
到根的路径上有 条公路未翻修,
j
j
条铁路未翻修, 的子树内所有乡村的最小不便利值。
边界:当
u
u
为乡村时,
转移也就是决定翻修由
u
u
发出的公路还是铁路:
最后答案:
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
typedef long long ll;
const int N = 4e4 + 5, Z = 43, W = 2e4 + 5;
int n, a[N], b[N], c[N], s[W], t[W];
ll f[W][Z][Z];
void dfs(int u) {
if (s[u] < n) dfs(s[u]); if (t[u] < n) dfs(t[u]);
int i, j; For (i, 0, 40) For (j, 0, 40) {
ll x1 = s[u] >= n ? 1ll * c[s[u]] * (a[s[u]] + i) * (b[s[u]] + j)
: f[s[u]][i][j],
x2 = t[u] >= n ? 1ll * c[t[u]] * (a[t[u]] + i) * (b[t[u]] + j + 1)
: f[t[u]][i][j + 1],
x3 = s[u] >= n ? 1ll * c[s[u]] * (a[s[u]] + i + 1) * (b[s[u]] + j)
: f[s[u]][i + 1][j],
x4 = t[u] >= n ? 1ll * c[t[u]] * (a[t[u]] + i) * (b[t[u]] + j)
: f[t[u]][i][j];
f[u][i][j] = min(x1 + x2, x3 + x4);
}
}
int main() {
int i, x, y; n = read(); For (i, 1, n - 1) {
s[i] = x = read(); t[i] = y = read();
if (x < 0) s[i] = n - 1 - x; if (y < 0) t[i] = n - 1 - y;
}
For (i, 1, n) a[n - 1 + i] = read(), b[n - 1 + i] = read(),
c[n - 1 + i] = read(); dfs(1);
cout << f[1][0][0] << endl; return 0;
}