You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.0 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
2 years ago
const int N = 1e5 + 10, M = N << 1;
2 years ago
#define int long long
#define endl "\n"
2 years ago
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
2 years ago
int c[N]; // 点权数组
2 years ago
2 years ago
int sz[N]; // sz[i]:在以1号节点为根时i号节点的子节点数量
int dis[N]; // dis[i]:表示i距离起点的长度
int f[N]; // f[i]:把奶牛大集会的地点设为i时的最小代价
int ans = 1e18;
// 第一次dfs,获取在以1为根的树中:
// 1、每个节点分别有多少个子节点填充sz[]数组
// 2、获取到f[1],f[1]表示在1点设置医院的代价
// 获取到上面这一组+一个数据才能进行dfs2进行换根
2 years ago
void dfs1(int u, int fa) {
2 years ago
sz[u] = c[u]; // 这个和医院选址是一样的,点权就是子节点个数
2 years ago
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
2 years ago
if (v == fa) continue;
2 years ago
dis[v] = dis[u] + w[i]; // 每个点到根节点的距离,这个和医院选址是不一样的,那个是一步+1用step记录即可这个还有边权
dfs1(v, u); // 深搜
sz[u] += sz[v]; // 以u为根的子树奶牛数量
2 years ago
}
2 years ago
f[1] += dis[u] * c[u]; // 累加 距离*人数=1点的总代价
2 years ago
}
2 years ago
// 第二次dfs,开始dp换根
2 years ago
void dfs2(int u, int fa) {
2 years ago
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
2 years ago
if (v == fa) continue;
2 years ago
f[v] = f[u] + (sz[1] - sz[v] * 2) * w[i];
2 years ago
dfs2(v, u);
}
}
signed main() {
2 years ago
// 初始化链式前向星
memset(h, -1, sizeof h);
2 years ago
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> c[i];
for (int i = 1; i < n; i++) {
2 years ago
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
2 years ago
}
2 years ago
// 1、准备动作
dfs1(1, 0);
// 2、换根dp
dfs2(1, 0);
// 输出答案
2 years ago
for (int i = 1; i <= n; i++) ans = min(ans, f[i]);
cout << ans << endl;
}