제출 #1124383

#제출 시각아이디문제언어결과실행 시간메모리
1124383ArtistWall통행료 (APIO13_toll)C++20
100 / 100
2118 ms5820 KiB
#include <bits/stdc++.h> #define long long long using namespace std; struct union_find { vector<int> s, p; union_find(int n = 0) : s(n, 1), p(n) { iota(p.begin(), p.end(), 0); } int find(int u) { if (p[u] != u) { p[u] = find(p[u]); } return p[u]; } bool merge(int u, int v) { u = find(u); v = find(v); if (u == v) { return false; } if (s[u] < s[v]) { swap(u, v); } p[v] = u; s[u] = s[u] + s[v]; return true; } bool same(int u, int v) { return find(u) == find(v); } int size(int u) { u = find(u); return s[u]; } }; const int N = (int) 1E5; const int M = (int) 3E5; const int K = 20; const int A = numeric_limits<int>::max(); int n, m, k; bool t1[M]; array<int, 3> e1[M]; array<int, 2> e2[K]; array<int, 3> e3[K]; vector<int> e4[K + 1]; int a[N]; long e[K + 1]; void Kruskal1() { union_find s(n); for (int i = 0; i < k; i++) { int u = e2[i][0], v = e2[i][1]; s.merge(u, v); } for (int i = 0; i < m; i++) { int u = e1[i][1], v = e1[i][2]; t1[i] = s.merge(u, v); } } int root; void Kruskal2() { union_find s1(n); for (int i = 0; i < m; i++) { int u = e1[i][1], v = e1[i][2]; if (t1[i] == true) { s1.merge(u, v); } } int c = 0; vector<int> p(n, -1); for (int i = 0; i < n; i++) { if (s1.find(i) == i) { p[i] = c; c = c + 1; } } for (int i = 0; i < k; i++) { int u = e2[i][0], v = e2[i][1]; u = p[s1.find(u)]; v = p[s1.find(v)]; e2[i] = {u, v}; } int t = 0; union_find s2(k + 1); for (int i = 0; i < m; i++) { int u = e1[i][1], v = e1[i][2], w = e1[i][0]; if (t1[i] == false) { int x = p[s1.find(u)]; int y = p[s1.find(v)]; if (s2.merge(x, y)) { e3[t] = {w, x, y}; t = t + 1; } } } assert(t + 1 == c); for (int i = 0; i < n; i++) { int u = p[s1.find(i)]; e[u] = e[u] + a[i]; } root = p[s1.find(0)]; } int p[K + 1], d[K + 1]; int g[K + 1]; long t[K + 1]; void DFS(int u) { t[u] = e[u]; for (auto v : e4[u]) { if (v != p[u]) { p[v] = u; d[v] = d[u] + 1; DFS(v); t[u] = t[u] + t[v]; } } } void check(int u, int v, int w) { while (u != v) { if (d[u] > d[v]) { swap(u, v); } g[v] = min(g[v], w); v = p[v]; } } void solve() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; u = u - 1, v = v - 1; e1[i] = {w, u, v}; } for (int i = 0; i < k; i++) { int u, v; cin >> u >> v; u = u - 1, v = v - 1; e2[i] = {u, v}; } for (int i = 0; i < n; i++) { cin >> a[i]; } sort(e1, e1 + m); Kruskal1(); Kruskal2(); long c = 0; for (int S = 0; S < (1 << k); S++) { union_find s(k + 1); bool f = true; for (int i = 0; i < k; i++) { if (S >> i & 1) { int u = e2[i][0], v = e2[i][1]; if (not s.merge(u, v)) { f = false; break; } } } if (not f) { continue; } vector<bool> t3(k); for (int i = 0; i < k; i++) { int u = e3[i][1], v = e3[i][2]; t3[i] = s.merge(u, v); } for (int i = 0; i < k + 1; i++) { e4[i] = vector<int>(); } for (int i = 0; i < k; i++) { if (S >> i & 1) { int u = e2[i][0], v = e2[i][1]; e4[u].push_back(v); e4[v].push_back(u); } } for (int i = 0; i < k; i++) { if (t3[i] == true) { int u = e3[i][1], v = e3[i][2]; e4[u].push_back(v); e4[v].push_back(u); } } p[root] = -1; d[root] = 0; DFS(root); fill(g, g + k + 1, A); for (int i = 0; i < k; i++) { if (t3[i] == false) { int u = e3[i][1], v = e3[i][2], w = e3[i][0]; check(u, v, w); } } long b = 0; for (int i = 0; i < k; i++) { if (S >> i & 1) { int u = e2[i][0], v = e2[i][1]; int x = d[u] > d[v] ? u : v; b = b + t[x] * g[x]; } } c = max(c, b); } cout << c << "\n"; } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; t = 1; for (int i = 0; i < t; i++) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...