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.
44 lines
1.2 KiB
44 lines
1.2 KiB
2 years ago
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
const int N = 110;
|
||
|
int n;
|
||
|
int a[N], b[N]; // b 数组存是否遍历过这个节点
|
||
|
int e[N][10]; // 存储树
|
||
|
int dis[N][N]; // 存节点间的距离
|
||
|
int cnt;
|
||
|
|
||
|
void dfs(int u, int x) { // x 表示起点
|
||
|
b[u] = 1;
|
||
|
dis[x][u] = cnt, dis[u][x] = cnt;
|
||
|
cnt++;
|
||
|
for (int i = 1; i <= e[u][0]; i++) { // 枚举子节点
|
||
|
if (b[e[u][i]] == 0) {
|
||
|
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 != 0) e[i][++e[i][0]] = x, e[x][++e[x][0]] = i; // 存图
|
||
|
if (y != 0) e[i][++e[i][0]] = y, e[y][++e[y][0]] = i;
|
||
|
}
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
for (int j = 1; j <= n; j++) b[j] = 0;
|
||
|
cnt = 0; // 初始化
|
||
|
dfs(i, i); // 搜索
|
||
|
}
|
||
|
int ans = 1e9;
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
int sum = 0;
|
||
|
for (int j = 1; j <= n; j++)
|
||
|
sum = sum + a[j] * dis[i][j]; // 累加距离
|
||
|
ans = min(ans, sum);
|
||
|
}
|
||
|
cout << ans;
|
||
|
return 0;
|
||
|
}
|