-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst_prim.cpp
90 lines (81 loc) · 1.46 KB
/
mst_prim.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
MST with Prim's algorithm
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 100000+21;
vector<pair<int, int> > graph[N];
int n, m, a, b, c;
int vis[N];
int edge[N];
vector<pair<pair<int, int>, int> > ans;
void prim()
{
int start = 0;
priority_queue<pair<int, pair<int, int> > > Q;
Q.push({0, {start, start}});
while (!Q.empty())
{
pair<int, pair<int, int> > curr = Q.top();
int curr_v = curr.second.first;
Q.pop();
if (vis[curr_v])
continue;
vis[curr_v] = true;
if (curr.second.second != curr.second.first)
ans.push_back({{curr.second.second, curr.second.first}, -curr.first});
for (int i=0; i<graph[curr_v].size(); i++)
{
int v = graph[curr_v][i].first;
int waga = graph[curr_v][i].second;
if (vis[v] != true)
{
Q.push({-waga, {v, curr_v}});
}
}
}
}
int main()
{
cin>>n>>m;
for (int i=0; i<m; i++)
{
cin>>a>>b>>c;
graph[a].push_back(make_pair(b, c));
graph[b].push_back(make_pair(a, c));
}
prim();
for (int i=0; i<ans.size(); i++)
{
cout<<ans[i].first.first<<" "<<ans[i].first.second<<" "<<ans[i].second<<endl;
}
}
/*
Example:
9 14
0 1 4
0 7 8
1 2 8
1 7 11
2 3 7
2 8 2
2 5 4
3 4 9
3 5 14
4 5 10
5 6 2
6 7 1
6 8 6
7 8 7
Answer:
0 1 4
0 7 8
7 6 1
6 5 2
5 2 4
2 8 2
2 3 7
3 4 9
*/