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.
41 lines
867 B
41 lines
867 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
const int N = 10010, M = N << 1;
|
|
int n;
|
|
int ans;
|
|
int res = INF;
|
|
|
|
// 邻接表
|
|
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++;
|
|
}
|
|
|
|
void dfs(int u, int fa, int sum) {
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int v = e[i];
|
|
if (v == fa) continue;
|
|
dfs(v, u, sum + w[i]);
|
|
}
|
|
ans = max(ans, sum);
|
|
}
|
|
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n;
|
|
for (int i = 1; i < n; i++) {
|
|
int a, b, c;
|
|
cin >> a >> b >> c;
|
|
add(a, b, c), add(b, a, c);
|
|
}
|
|
// 暴力换根
|
|
for (int i = 1; i <= n; i++) {
|
|
ans = 0;
|
|
dfs(i, 0, 0);
|
|
res = min(res, ans);
|
|
}
|
|
printf("%d\n", res);
|
|
return 0;
|
|
} |