-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmst_prim.cpp
More file actions
41 lines (33 loc) · 829 Bytes
/
mst_prim.cpp
File metadata and controls
41 lines (33 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
int n,m,ans;
vector<bool> visited;
vector<vector<pair<int,int> > > adj;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
adj.resize(n);
visited.resize(n);
for(int i = 0;i < m;i++)
{
int a,b,d;
cin >> a >> b >> d;
adj[a].push_back({d,b});
adj[b].push_back({d,a});
}
q.push({0,0});
while(!q.empty())
{
int w = q.top().first,p = q.top().second;
q.pop();
if(visited[p]) continue;
visited[p] = true;
ans+=w;
for(int i = 0;i < adj[p].size();i++) if(!visited[adj[p][i].second]) q.push(adj[p][i]);
}
cout << ans;
}