Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define D(x) cout<<#x<<" = "<<x<<endl
- #define inf 1e9
- #define mx 200000
- int n, m, d, flag = 0, sum = 0;
- vector<int> edge[mx+5];
- vector<int> cost[mx+5];
- int value[mx+5], people[mx+5], visited[mx+5];
- struct node
- {
- int x, d;
- }nd;
- bool operator < (node A , node B)
- {
- if(A.d < B.d) return false;
- else return true;
- }
- void init()
- {
- for(int i =0 ;i< mx+5;i++)
- {
- value[i] = inf;
- cost[i].clear();
- edge[i].clear();
- }
- memset(visited,0, sizeof visited);
- }
- void mst(int src)
- {
- value[src] = 0;
- if(flag) value[src] = d; flag = 1;
- priority_queue<node> q;
- q.push({src, value[src]});
- while(!q.empty())
- {
- node u = q.top();
- visited[u.x] = 1;
- q.pop();
- for(int i = 0;i< edge[u.x].size() ;i++)
- {
- node v;
- v.x = edge[u.x][i];
- v.d = cost[u.x][i];
- if(!visited[v.x] && people[v.x] && value[v.x] > v.d)
- {
- value[v.x] = v.d;
- q.push(v);
- }
- }
- }
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- int u, v, w;
- init();
- scanf("%d%d%d",&n,&m,&d);
- for(int i = 1;i<= n;i++) scanf("%d",&people[i]);
- while(m--)
- {
- scanf("%d%d%d",&u,&v,&w);
- edge[u].push_back(v);
- edge[v].push_back(u);
- cost[u].push_back(w);
- cost[v].push_back(w);
- }
- for(int i = 1;i<=n;i++)
- {
- if(!visited[i] && people[i]) mst(i);
- }
- for(int i =1 ;i<= n;i++)
- if(value[i] != inf) sum+=value[i];
- printf("%d\n",sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement