题目
题目太长懒得打
题解
HNOI2018惊现普及+/提高?
由最长路径很短,设(f[i][x][y])表示(i)号点到根有(x)条未修公路,(y)条未修铁路,子树所有乡村不便利值的最小值
如果(i)为乡村,直接等于公式
如果(i)不为乡村,枚举修哪边儿子
(f[i][x][y] = min{f[ls][x + 1][y] + f[rs][x][y],f[ls][x][y] + f[rs][x][y + 1]})
(ans = f[1][0][0])
很多(f)用完就丢了,可以滚一下数组省空间
完了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 40005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
LL f[110][41][41];
int n,s[maxn],t[maxn],a[maxn],b[maxn],c[maxn],dep[maxn];
int st[maxn],top,id[maxn];
void dfs(int u){
if (u >= n){
int x = u - n + 1; id[u] = st[top--];
for (int i = 0; i <= dep[u]; i++)
for (int j = 0; j <= dep[u]; j++)
f[id[u]][i][j] = 1ll * c[x] * (a[x] + i) * (b[x] + j);
return;
}
dep[s[u]] = dep[t[u]] = dep[u] + 1;
dfs(s[u]); dfs(t[u]);
id[u] = st[top--];
for (int i = 0; i <= dep[u]; i++)
for (int j = 0; j <= dep[u]; j++)
f[id[u]][i][j] = min(f[id[s[u]]][i][j] + f[id[t[u]]][i][j + 1],f[id[s[u]]][i + 1][j] + f[id[t[u]]][i][j]);
st[++top] = id[s[u]]; st[++top] = id[t[u]];
}
int main(){
n = read();
for (int i = 1; i < n; i++){
s[i] = read(); t[i] = read();
if (s[i] < 0) s[i] = -s[i] + n - 1;
if (t[i] < 0) t[i] = -t[i] + n - 1;
}
for (int i = 1; i <= n; i++) a[i] = read(),b[i] = read(),c[i] = read();
top = 99;
REP(i,top) st[i] = i;
dfs(1);
printf("%lld
",f[id[1]][0][0]);
return 0;
}