#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 2e5;
std::vector<std::pair<int, int>> e[N];
int n, upedge[N];
i64 dp[N];
void dfs(int u, int p) {
dp[u] = 0;
for (auto [v, w] : e[u]) {
if (v == p) {
upedge[u] = w;
}
}
for (auto [v, w] : e[u]) {
if (v != p) {
dfs(v, u);
dp[u] += dp[v] + upedge[v];
}
}
}
i64 ans[N];
void dfs2(int u, int p, i64 car = 0) {
ans[u] = dp[0] + car;
for (auto [v, w] : e[u]) {
if (v != p) {
dfs2(v, u, car + w - upedge[v]);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
i64 sum = 0;
for (int i = 0; i < n - 1; i++) {
int u, v, c, d;
std::cin >> u >> v >> c >> d;
u--;
v--;
e[u].emplace_back(v, c);
e[v].emplace_back(u, d);
sum += c + d;
}
dfs(0, 0);
dfs2(0, 0);
i64 res = *std::max_element(ans, ans + n);
std::cout << sum - res << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
247 ms |
16232 KB |
Output is correct |
3 |
Correct |
267 ms |
31688 KB |
Output is correct |
4 |
Correct |
234 ms |
21308 KB |
Output is correct |
5 |
Correct |
232 ms |
22612 KB |
Output is correct |
6 |
Correct |
239 ms |
23888 KB |
Output is correct |
7 |
Correct |
200 ms |
22720 KB |
Output is correct |
8 |
Correct |
283 ms |
32036 KB |
Output is correct |
9 |
Correct |
154 ms |
23172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
247 ms |
16232 KB |
Output is correct |
3 |
Correct |
267 ms |
31688 KB |
Output is correct |
4 |
Correct |
234 ms |
21308 KB |
Output is correct |
5 |
Correct |
232 ms |
22612 KB |
Output is correct |
6 |
Correct |
239 ms |
23888 KB |
Output is correct |
7 |
Correct |
200 ms |
22720 KB |
Output is correct |
8 |
Correct |
283 ms |
32036 KB |
Output is correct |
9 |
Correct |
154 ms |
23172 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |