mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 17:28:47 +00:00
1827. [Usaco2010 Mar]gather 奶牛大集会
https://hydro.ac/d/bzoj/record/6322ae5fbdf9bc31d1606279
This commit is contained in:
parent
6b06184d03
commit
0ef4b0e96c
81
BZOJ/1827/1827.cpp
Normal file
81
BZOJ/1827/1827.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e5 + 5;
|
||||
|
||||
int n, sum, root, c[N], siz[N], max_siz[N], dep[N];
|
||||
long long ans;
|
||||
std::vector<std::pair<int, int>> g[N];
|
||||
|
||||
void dfs1(int u, int f) {
|
||||
siz[u] = c[u];
|
||||
|
||||
for (auto e : g[u]) {
|
||||
int v = e.first,
|
||||
w = e.second;
|
||||
|
||||
if (v == f) continue;
|
||||
|
||||
dfs1(v, u);
|
||||
|
||||
siz[u] += siz[v];
|
||||
max_siz[u] = std::max(max_siz[u], siz[v]);
|
||||
}
|
||||
|
||||
max_siz[u] = std::max(max_siz[u], sum - siz[u]);
|
||||
}
|
||||
|
||||
void dfs2(int u, int f) {
|
||||
for (auto e : g[u]) {
|
||||
int v = e.first,
|
||||
w = e.second;
|
||||
|
||||
if (v == f) continue;
|
||||
|
||||
dep[v] = dep[u] + w;
|
||||
dfs2(v, u);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> c[i];
|
||||
|
||||
sum += c[i];
|
||||
}
|
||||
|
||||
for (int i = 1, u, v, w; i <= n; i++) {
|
||||
cin >> u >> v >> w;
|
||||
|
||||
g[u].emplace_back(v, w);
|
||||
g[v].emplace_back(u, w);
|
||||
}
|
||||
|
||||
dfs1(1, 0);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (!root || max_siz[i] < max_siz[root]) {
|
||||
root = i;
|
||||
}
|
||||
}
|
||||
|
||||
dep[0] = -1;
|
||||
dfs2(root, 0);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
ans += static_cast<long long>(c[i]) * dep[i];
|
||||
}
|
||||
|
||||
cout << ans << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
BZOJ/1827/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/10.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/10.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/6.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/6.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/7.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/7.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/8.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/8.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/9.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1827/data/9.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1827/data/9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user