1
0

CodeForces-1095F.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <tuple>
  5. #include <vector>
  6. using namespace std;
  7. struct DSU {
  8. vector<int> p, s;
  9. explicit DSU(int n) : p(n), s(n, 1) { iota(p.begin(), p.end(), 0); }
  10. int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
  11. bool unite(int a, int b) {
  12. a = find(a); b = find(b); if (a == b) return false;
  13. if (s[a] < s[b]) swap(a, b); p[b] = a; s[a] += s[b]; return true;
  14. }
  15. };
  16. int main() {
  17. ios::sync_with_stdio(false);
  18. cin.tie(nullptr);
  19. int n, m; cin >> n >> m;
  20. vector<long long> a(n);
  21. for (auto &x : a) cin >> x;
  22. vector<tuple<long long, int, int>> edges;
  23. edges.reserve(m + n - 1);
  24. while (m--) {
  25. int u, v; long long w; cin >> u >> v >> w;
  26. edges.push_back({w, u - 1, v - 1});
  27. }
  28. int hub = int(min_element(a.begin(), a.end()) - a.begin());
  29. for (int v = 0; v < n; ++v) if (v != hub) edges.push_back({a[hub] + a[v], hub, v});
  30. sort(edges.begin(), edges.end());
  31. DSU dsu(n); long long answer = 0;
  32. for (auto [w, u, v] : edges) if (dsu.unite(u, v)) answer += w;
  33. cout << answer << '\n';
  34. }