Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pii pair<long long,int>
- vector<pii>v[100007];
- bool vis[100007];
- long long solve(int x)
- {
- int y;
- long long min_cost=0;
- priority_queue<pii>p;
- p.push({0,x});
- while(!p.empty())
- {
- pii p1=p.top();
- p.pop();
- x=p1.second;
- if(vis[x])continue;
- min_cost-=p1.first;
- vis[x]=true;
- for(int i=0;i<v[x].size();i++)
- {
- if(!vis[v[x][i].second])
- {
- p.push({-1*v[x][i].first,v[x][i].second});
- }
- }
- }
- return min_cost;
- }
- int main()
- {
- int x,y,nodes,edges;
- long long cost;
- cin>>nodes>>edges;
- for(int i=0;i<edges;i++)
- {
- cin>>x>>y>>cost;
- v[x].push_back({cost,y});
- v[y].push_back({cost,x});
- }
- cout<<solve(1)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement