#include <bits/stdc++.h>
using namespace std;
using pii = pair <int, int>;
struct edge {
int l, r, cost;
edge () {}
edge (int l, int r, int cost) : l(l), r(r), cost(cost) {}
bool operator < (edge e) const {
return cost < e.cost;
}
} e[300010], sp[30];
int p[100010];
int val[30];
int n, m, k;
vector <pii> g[100010];
int sub[100010];
long long total;
int par[100010];
int sz[100010];
int root(int x) {
if(x == par[x]) return par[x];
return par[x] = root(par[x]);
}
void join(int x, int y) {
x = root(x);
y = root(y);
if(x != y) {
if(sz[x] > sz[y]) swap(x, y);
sz[y] += sz[x];
par[x] = y;
}
}
void dfs(int x, int par) {
sub[x] = p[x];
for(auto y : g[x]) {
int i = y.first;
if(i != par) {
dfs(i, x);
sub[x] += sub[i];
total += 1LL * y.second * sub[i];
}
}
}
long long solve(vector <int> take) {
for(int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
g[i].clear();
}
auto add_edge = [&] (int i, int j, int type) {
g[i].emplace_back(j, type);
g[j].emplace_back(i, type);
};
for(int i : take) {
int l, r;
tie(l, r) = make_pair(sp[i].l, sp[i].r);
if(root(l) == root(r)) return 0;
join(l, r);
add_edge(l, r, val[i]);
}
for(int i = 0; i < m; i++) {
int l = e[i].l;
int r = e[i].r;
if(root(l) != root(r)) {
join(l, r);
add_edge(l, r, 0);
}
}
total = 0;
dfs(1, 0);
return total;
}
int main() {
scanf("%d %d %d", &n, &m, &k);
for(int i = 0; i < m; i++) {
scanf("%d %d %d", &e[i].l, &e[i].r, &e[i].cost);
}
sort(e, e + m);
for(int i = 0; i < k; i++) {
scanf("%d %d", &sp[i].l, &sp[i].r);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
for(int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
}
for(int i = 0; i < m; i++) {
int l, r;
tie(l, r) = make_pair(e[i].l, e[i].r);
if(root(l) != root(r)) {
join(l, r);
g[l].emplace_back(r, e[i].cost);
g[r].emplace_back(l, e[i].cost);
}
}
vector <int> dad (n + 1, 0);
vector <int> pval (n + 1, 0);
vector <int> dep (n + 1, 0);
function <void(int, int)> getPar = [&] (int x, int prev) {
dad[x] = prev;
for(auto y : g[x]) {
int i = y.first;
if(i != prev) {
dep[i] = 1 + dep[x];
getPar(i, x);
pval[i] = y.second;
}
}
};
getPar(1, 0);
for(int i = 0; i < k; i++) {
int l, r;
tie(l, r) = make_pair(sp[i].l, sp[i].r);
if(dep[l] > dep[r]) swap(l, r);
while(dep[l] < dep[r]) {
val[i] = max(val[i], pval[r]);
r = dad[r];
}
while(l != r) {
val[i] = max(val[i], pval[l]);
val[i] = max(val[i], pval[r]);
l = dad[l]; r = dad[r];
}
}
long long ans = 0;
for(int i = 0; i < (1 << k); i++) {
vector <int> take;
for(int j = 0; j < k; j++) {
if((i >> j) & 1) {
take.push_back(j);
}
}
ans = max(ans, solve(take));
}
printf("%lld\n", ans);
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &m, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &e[i].l, &e[i].r, &e[i].cost);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &sp[i].l, &sp[i].r);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
7 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
7 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
7 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
7 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |