# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
726318 |
2023-04-18T17:53:19 Z |
vjudge1 |
Toll (APIO13_toll) |
C++17 |
|
2500 ms |
11036 KB |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[300000], b[300000], c[300000], ord[300000], cnt[300000];
int x[20], y[20], cost[20];
int id[100000];
int64_t p[100000], pp[100000];
int64_t par[100000];
vector<pair<int, int>> adj[100000];
inline int root(int u) { return par[u] < 0 ? u : par[u] = root(par[u]); }
inline bool is_connected(int u, int v) { return root(u) == root(v); }
inline bool unite(int u, int v) {
u = root(u), v = root(v);
if (u == v) return 0;
if (par[u] > par[v]) swap(u, v);
p[u] += p[v];
par[u] += par[v];
par[v] = u;
return 1;
}
int64_t ans;
int64_t dfs(int u, int pr) {
int64_t res = 0;
for (pair<int, int>& e : adj[u]) {
if (e.first == pr) continue;
int64_t dpv = dfs(e.first, u);
res += dpv;
ans += 1ll * dpv * e.second;
}
return res + p[u];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--;
for (int i = 0; i < k; i++) cin >> x[i] >> y[i], x[i]--, y[i]--;
for (int i = 0; i < n; i++) cin >> pp[i];
memset(par, -1, n * sizeof *par);
iota(ord, ord + m, 0);
sort(ord, ord + m, [&](int a, int b) {
return c[a] < c[b];
});
for (int j = 0; j < m; j++) {
cnt[ord[j]] += unite(a[ord[j]], b[ord[j]]);
} // spanning tree with 0 new edge
memset(par, -1, n * sizeof *par);
for (int i = 0; i < k; i++) {
unite(x[i], y[i]);
}
for (int j = 0; j < m; j++) {
cnt[ord[j]] += unite(a[ord[j]], b[ord[j]]);
} // spanning tree with k new edges
vector<int> almost_mst;
memset(par, -1, n * sizeof *par);
for (int i = 0; i < n; i++) p[i] = pp[i];
for (int i = 0; i < m; i++) {
if (cnt[ord[i]] == 1) almost_mst.emplace_back(ord[i]);
if (cnt[ord[i]] == 2) unite(a[ord[i]], b[ord[i]]);
}
int N = 0;
for (int i = 0; i < n; i++) {
if (root(i) == i) id[i] = N, pp[N++] = p[i];
}
for (int j = 0; j < k; j++) x[j] = id[root(x[j])];
for (int j = 0; j < k; j++) y[j] = id[root(y[j])];
for (int j : almost_mst) a[j] = id[root(a[j])];
for (int j : almost_mst) b[j] = id[root(b[j])];
const int ROOT = id[root(0)];
int64_t res = 0;
for (int i = 1; i < 1 << k; i++) {
memset(par, -1, N * sizeof *par);
for (int j = 0; j < N; j++) p[j] = pp[j];
bool ok = 1;
for (int j = 0; j < k; j++) {
if (i >> j & 1) {
if (!unite(x[j], y[j])) ok = 0;
}
}
if (!ok) continue;
vector<int> not_mst, mst;
for (int j : almost_mst) {
if (!unite(a[j], b[j])) {
not_mst.emplace_back(j);
} else {
mst.emplace_back(j);
}
}
for (int j = 0; j < k; j++) {
memset(par, -1, N * sizeof *par);
for (int j = 0; j < N; j++) p[j] = pp[j];
for (int j : mst) unite(a[j], b[j]);
if (i >> j & 1) {
for (int z = 0; z < k; z++) {
if ((i >> z & 1) && j != z) unite(x[z], y[z]);
}
}
cost[j] = 1e9;
for (int z : not_mst) {
if (!is_connected(a[z], b[z])) cost[j] = min(cost[j], c[z]);
}
}
memset(par, -1, N * sizeof *par);
for (int j = 0; j < N; j++) p[j] = pp[j];
for (int j : mst) unite(a[j], b[j]);
for (int j = 0; j < k; j++) {
if (i >> j & 1) {
adj[root(x[j])].emplace_back(root(y[j]), cost[j]);
adj[root(y[j])].emplace_back(root(x[j]), cost[j]);
}
}
ans = 0;
dfs(root(ROOT), -1);
res = max(res, ans);
for (int j = 0; j < k; j++) {
if (i >> j & 1) {
adj[root(x[j])].pop_back();
adj[root(y[j])].pop_back();
}
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
6 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
6 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
7 |
Correct |
259 ms |
11036 KB |
Output is correct |
8 |
Correct |
272 ms |
10816 KB |
Output is correct |
9 |
Correct |
295 ms |
10936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
6 ms |
2772 KB |
Output is correct |
6 |
Correct |
5 ms |
2772 KB |
Output is correct |
7 |
Correct |
259 ms |
11036 KB |
Output is correct |
8 |
Correct |
272 ms |
10816 KB |
Output is correct |
9 |
Correct |
295 ms |
10936 KB |
Output is correct |
10 |
Execution timed out |
2550 ms |
10968 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |