对每个节点维护一个 node,nodeans 是答案,nodeans1 是 ∑ci×(bi+y),nodeans2 是 ∑ci×(ai+x),nodesum 是 ∑ci,转移见代码。
// Expected: 100pts.
#include<bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 10;
using ll = long long;
int n, a[maxN], b[maxN], c[maxN], ch[maxN][2];
struct Node {
ll ans, ans1, ans2, sum;
inline Node operator + (const Node& rhs) const {
Node res;
res.ans = ans + rhs.ans; res.ans1 = ans1 + rhs.ans1;
res.ans2 = ans2 + rhs.ans2; res.sum = sum + rhs.sum;
return res;
}
inline bool operator < (const Node& rhs) const { return ans < rhs.ans; }
} nodes[maxN];
void dfs(int u) {
if(u > n) {
u -= n;
return void(nodes[u + n] = {
1ll * c[u] * a[u] * b[u],
1ll * c[u] * b[u],
1ll * c[u] * a[u],
c[u]
});
}
dfs(ch[u][0]), dfs(ch[u][1]);
// 翻新公路,右侧铁路不便利值增大
Node rr = nodes[ch[u][1]]; rr.ans += rr.ans2; rr.ans1 += rr.sum;
// 翻新铁路,左侧公路不便利值增大
Node ll = nodes[ch[u][0]]; ll.ans += ll.ans1; ll.ans2 += ll.sum;
nodes[u] = min(ll + nodes[ch[u][1]], rr + nodes[ch[u][0]]);
}
signed main() {
cin >> n;
for(int i = 1; i < n; i++) {
cin >> ch[i][0] >> ch[i][1];
if(ch[i][0] < 0) ch[i][0] = n - ch[i][0];
if(ch[i][1] < 0) ch[i][1] = n - ch[i][1];
}
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
dfs(1);
cout << nodes[1].ans;
}