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.
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 110;
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
int n;
|
|
|
|
int a[N], st[N]; // b 数组存是否遍历过这个节点
|
|
|
|
int e[N][10]; // 存储树
|
|
|
|
int dis[N][N]; // 存节点间的距离
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
void dfs(int u, int x) { // x 表示起点
|
|
|
|
st[u] = 1;
|
|
|
|
dis[x][u] = cnt, dis[u][x] = cnt;
|
|
|
|
cnt++;
|
|
|
|
for (int i = 1; i <= e[u][0]; i++) { // 枚举子节点
|
|
|
|
if (st[e[u][i]]) continue;
|
|
|
|
dfs(e[u][i], x);
|
|
|
|
}
|
|
|
|
cnt--; // 退回上一个节点,要记得把距离减去一
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
cin >> n;
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
int x, y;
|
|
|
|
cin >> a[i] >> x >> y;
|
|
|
|
if (x) e[i][++e[i][0]] = x, e[x][++e[x][0]] = i; // 存图
|
|
|
|
if (y) e[i][++e[i][0]] = y, e[y][++e[y][0]] = i;
|
|
|
|
}
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
memset(st, 0, sizeof st);
|
|
|
|
cnt = 0; // 初始化
|
|
|
|
dfs(i, i); // 搜索
|
|
|
|
}
|
|
|
|
int ans = INF;
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
int s = 0;
|
|
|
|
for (int j = 1; j <= n; j++)
|
|
|
|
s = s + a[j] * dis[i][j]; // 累加距离
|
|
|
|
ans = min(ans, s);
|
|
|
|
}
|
|
|
|
cout << ans << endl;
|
|
|
|
return 0;
|
|
|
|
}
|