SuitNdtie

COI great raid

Mar 28th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. typedef long long int ll;
  5.  
  6. struct edge{
  7.     int u,v;
  8.     ll dif;
  9.     bool operator < (const edge& rhs)const
  10.     {
  11.         return dif > rhs.dif;
  12.     }
  13. };
  14.  
  15. int set[100010];
  16. int find(int x){
  17.     if(set[x] == x)return x;
  18.     return set[x] = find(set[x]);
  19. }
  20. void unions(int x,int y){
  21.     int rx = find(x);
  22.     int ry = find(y);
  23.     set[rx] = ry;
  24. }
  25. bool isset(int x,int y){
  26.     return find(x) == find(y);
  27. }
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33.     for(int i=0;i<=100000;i++)set[i] = i;
  34.     int n,m;
  35.     scanf("%d",&n);
  36.     int str[n+1];
  37.     for(int i=1;i<=n;i++)scanf("%lld",&str[i]);
  38.     scanf("%d",&m);
  39.    
  40.     priority_queue<edge> pq;
  41.     for(int i=1;i<=m;i++){
  42.         int u,v;
  43.         scanf("%d %d",&u,&v);
  44.         pq.push({u,v,str[u] + str[v]});
  45.     }
  46.     ll sum = 0;
  47. //  printf("%d\n",pq.size());
  48.     while(!pq.empty()){
  49.         int u = pq.top().u;
  50.         int v = pq.top().v;
  51.         ll t = pq.top().dif;
  52.         pq.pop();
  53.     //  printf("Test %d %d %lld\n",u,v,t);
  54.         if(isset(u,v))continue;
  55.         sum += t;
  56.         unions(u,v);
  57.     }
  58.     printf("%lld",sum);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment