#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using pil = pair<int, long long>;
using pli = pair<long long, int>;
const int N = 1e5 + 10;
struct Edge {
int u, v;
long long w;
bool operator<(const Edge &e) const {
return w < e.w;
}
};
struct DSU {
vector<int> dsu;
DSU(int n): dsu(n) {
for (int i = 1; i < n; i++)
dsu[i] = i;
}
int root(int idx) {
while (idx != dsu[idx]) {
dsu[idx] = dsu[dsu[idx]];
idx = dsu[idx];
}
return idx;
}
bool join(int a, int b) {
a = root(a);
b = root(b);
if (a == b)
return false;
dsu[b] = a;
return true;
}
};
struct Tree {
vector<vector<int>> adjList;
vector<long long> cnt;
bool valid = true;
DSU dsu;
Tree(int sz, vector<long long> &a):
adjList(sz + 1), dsu(sz + 1), cnt(a) {};
void addEdge(int u, int v) {
adjList[u].emplace_back(v);
adjList[v].emplace_back(u);
valid = valid && dsu.join(u, v);
}
void dfs(int v, int p) {
for (int u : adjList[v]) {
if (u == p) continue;
dfs(u, v);
cnt[v] += cnt[u];
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<Edge> edges, nedges;
int ai, bi;
long long ci;
for (int i = 0; i < m; i++) {
cin >> ai >> bi >> ci;
edges.push_back({ai, bi, ci});
}
sort(edges.begin(), edges.end());
int xi, yi;
for (int i = 0; i < k; i++) {
cin >> xi >> yi;
nedges.push_back({xi, yi, 0});
}
vector<long long> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
DSU dsu(n + 1), ndsu(n + 1);
for (auto [u, v, w] : nedges) {
ndsu.join(u, v);
}
vector<long long> mnw = {0};
for (auto [u, v, w] : edges) {
u = dsu.root(u);
v = dsu.root(v);
if (ndsu.root(u) != ndsu.root(v)) {
dsu.join(u, v);
}
else {
mnw.push_back(w);
}
}
sort(mnw.begin(), mnw.end());
int sz = 0;
vector<int> comp(n + 1, 0);
vector<long long> p = {0};
for (int i = 1; i <= n; i++) {
int v = dsu.root(i);
if (!comp[v]) {
comp[v] = ++sz;
p.push_back(0);
}
p[comp[v]] += a[i];
comp[i] = comp[v];
}
for (auto &[u, v, w] : nedges) {
u = comp[u];
v = comp[v];
}
int cbm = (1 << k);
long long ans = 0;
for (int bm = 1; bm < cbm; bm++) {
if (__builtin_popcount(bm) < sz - 1)
continue;
Tree g(sz, p);
for (int i = 0; i < k; i++) {
if (!(bm & (1 << i))) continue;
g.addEdge(nedges[i].u, nedges[i].v);
}
if (!g.valid)
continue;
g.dfs(1, -1);
vector<long long> cnt = g.cnt;
sort(cnt.begin(), cnt.end());
long long can = 0;
for (int i = 1; i < sz; i++) {
can += mnw[i] * cnt[i];
}
ans = max(ans, can);
}
cout << ans << '\n';
return 0;
}
Compilation message
toll.cpp: In constructor 'Tree::Tree(int, std::vector<long long int>&)':
toll.cpp:46:6: warning: 'Tree::dsu' will be initialized after [-Wreorder]
46 | DSU dsu;
| ^~~
toll.cpp:44:20: warning: 'std::vector<long long int> Tree::cnt' [-Wreorder]
44 | vector<long long> cnt;
| ^~~
toll.cpp:48:2: warning: when initialized here [-Wreorder]
48 | Tree(int sz, vector<long long> &a):
| ^~~~
toll.cpp: In function 'int main()':
toll.cpp:97:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
97 | for (auto [u, v, w] : nedges) {
| ^
toll.cpp:102:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
102 | for (auto [u, v, w] : edges) {
| ^
toll.cpp:128:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
128 | for (auto &[u, v, w] : nedges) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |