This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 1E5 + 5;
int par[MAXN];
struct DSU {
int n;
DSU(int n) : n(n) {
iota(par, par + n, 0);
}
int get(int a) {
return par[a] = (par[a] == a ? a : get(par[a]));
}
bool same(int a, int b) {
return get(a) == get(b);
}
bool unite(int a, int b) {
if(same(a, b)) {
return false;
}
a = get(a), b = get(b);
par[b] = a;
return true;
}
};
vector <vector <int>> adj;
#define ONLINE_JUDGE
void solve() {
int n, m, k;
cin >> n >> m >> k;
using T = tuple <int, int, int>;
vector <T> edg(m);
for(auto &[u, v, c] : edg) {
cin >> u >> v >> c;
}
sort(edg.begin(), edg.end(), [&](const T &a, const T &b) -> bool {
return get <2> (a) < get <2> (b);
});
vector <pair <int, int>> greedy(k);
for(auto &[u, v] : greedy) {
cin >> u >> v;
}
vector <int> p(n +1);
for(int i = 1; i <= n; i++) {
cin >> p[i];
}
i64 ans = 0;
for(int mask = 1; mask < (1 << k); mask++) {
DSU dsu(n +1);
adj.clear();
adj.assign(n +1, vector <int> ());
bool ok = true;
for(int i = 0; i < k; i++) {
if(mask & (1 << i)) {
auto [u, v] = greedy[i];
if(dsu.unite(u, v)) {
adj[u].emplace_back(v);
adj[v].emplace_back(u);
} else {
ok = false;
break;
}
}
}
if(!ok) {
continue;
}
int mn = int(1E9);
for(auto [u, v, c] : edg) {
if(dsu.unite(u, v)) {
adj[u].emplace_back(v);
adj[v].emplace_back(u);
} else {
mn = min(mn, c);
}
}
vector <i64> sub(n +1);
function <i64(int, int)> dfs = [&](int node, int par) -> i64 {
i64 res = p[node];
for(int &child : adj[node]) {
if(child != par) {
res += dfs(child, node);
}
}
return sub[node] = res;
};
dfs(1, 0);
vector <i64> anss;
i64 calc = 0;
for(int i = 0; i < k; i++) {
if(mask & (1 << i)) {
auto [u, v] = greedy[i];
anss.emplace_back(min(sub[u], sub[v]));
}
}
sort(anss.begin(), anss.end());
for(int i = 0; i < int(anss.size()); i++) {
calc += mn * anss[i];
}
ans = max(ans, calc);
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |