SuitNdtie

Worker

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