Naxocist

CCTV

May 7th, 2022 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using pi = pair<int, int>;
  6. using tiii = tuple<int, int, int>;
  7.  
  8. const int N = 1e5 + 3;
  9. int dsu[N];
  10. vector<tiii> mst, edge;
  11.  
  12. int par(int u){
  13.     return (dsu[u] == u ? u : dsu[u] = par(dsu[u]));
  14. }
  15.  
  16.  
  17. void un(int u, int v){
  18.     int x = par(u), y = par(v);
  19.     dsu[x] = y;
  20.  
  21.     return ;
  22. }
  23.  
  24.  
  25. int main() {
  26.     // freopen("input.in", "r", stdin);
  27.     int n, m, k; scanf("%d%d%d", &n, &m, &k);
  28.     for(int i=0; i<n; ++i) dsu[i] = i;
  29.    
  30.     int u, v, w;
  31.     for(int i=0; i<m; ++i){
  32.         scanf("%d%d%d", &u, &v, &w);
  33.         edge.emplace_back(w, u, v);
  34.     }
  35.  
  36.     sort(edge.begin(), edge.end());
  37.  
  38.     priority_queue<int> pq;
  39.  
  40.     for(auto x : edge){
  41.         tie(w, u, v) = x;
  42.         if(par(u) != par(v)){
  43.             un(u, v);
  44.             pq.push(w);
  45.         }
  46.     }
  47.  
  48.     int s = 0;
  49.  
  50.     while(k--){
  51.         int x = pq.top(); pq.pop();
  52.         x /= 2;
  53.         pq.push(x);
  54.     }
  55.  
  56.     while(pq.size()) s += pq.top(), pq.pop();
  57.  
  58.     printf("%d", s);
  59.     return 0;
  60. }
  61.  
Add Comment
Please, Sign In to add comment