Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define D(x) cout<<#x<<" = "<<x<<endl
  4. #define inf 1e9
  5. #define mx 200000
  6.  
  7. int n, m, d, flag = 0, sum = 0;
  8.  
  9. vector<int> edge[mx+5];
  10. vector<int> cost[mx+5];
  11.  
  12. int value[mx+5], people[mx+5], visited[mx+5];
  13.  
  14. struct node
  15. {
  16.     int x, d;
  17. }nd;
  18.  
  19. bool operator < (node A , node B)
  20. {
  21.     if(A.d < B.d) return false;
  22.     else return true;
  23. }
  24.  
  25.  
  26. void init()
  27. {
  28.     for(int i =0 ;i< mx+5;i++)
  29.     {
  30.         value[i] = inf;
  31.         cost[i].clear();
  32.         edge[i].clear();
  33.     }
  34.     memset(visited,0, sizeof visited);
  35.  
  36. }
  37.  
  38. void mst(int src)
  39. {
  40.     value[src] = 0;
  41.     if(flag) value[src] = d; flag = 1;
  42.     priority_queue<node> q;
  43.     q.push({src, value[src]});
  44.     while(!q.empty())
  45.     {
  46.         node u = q.top();
  47.         visited[u.x] = 1;
  48.         q.pop();
  49.         for(int i = 0;i< edge[u.x].size() ;i++)
  50.         {
  51.             node v;
  52.             v.x = edge[u.x][i];
  53.             v.d = cost[u.x][i];
  54.             if(!visited[v.x] && people[v.x] && value[v.x] > v.d)
  55.             {
  56.                 value[v.x] = v.d;
  57.                 q.push(v);
  58.             }
  59.         }
  60.     }
  61.  
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67.     //freopen("input.txt","r",stdin);
  68.     int u, v, w;
  69.     init();
  70.     scanf("%d%d%d",&n,&m,&d);
  71.     for(int i = 1;i<= n;i++) scanf("%d",&people[i]);
  72.     while(m--)
  73.     {
  74.         scanf("%d%d%d",&u,&v,&w);
  75.         edge[u].push_back(v);
  76.         edge[v].push_back(u);
  77.         cost[u].push_back(w);
  78.         cost[v].push_back(w);
  79.  
  80.     }
  81.     for(int i = 1;i<=n;i++)
  82.     {
  83.         if(!visited[i] && people[i]) mst(i);
  84.     }
  85.     for(int i =1 ;i<= n;i++)
  86.         if(value[i] != inf) sum+=value[i];
  87.     printf("%d\n",sum);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement