-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmst_fast_one.cpp
73 lines (60 loc) · 1.17 KB
/
mst_fast_one.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
#include<iostream>
#include<queue>
#define MAX 100000
using namespace std;
const int INF = 0x3f3f3f3f;
int pai[MAX];
struct Aresta {
int a, b, w;
Aresta(int a, int b, int w): a(a), b(b), w(w) {}
};
struct comp {
bool operator()(Aresta const& a, Aresta const& b) {
return a.w > b.w;
}
};
vector<Aresta> mst;
priority_queue<Aresta, vector<Aresta>, comp> pq;
int n, m, a , b, w;
int find(int x)
{
if(pai[x]==x)
return x;
return find(pai[x]);
}
void join(int x, int y)
{
x = find(x);
y = find(y);
pai[x] = y;
}
void kruskal()
{
for(int i = 1; i <= m; ++i)
{
pai[i] = i;
}
for(int i = 1; i <= m; ++i)
{
Aresta aresta = pq.top();
pq.pop();
if( find(aresta.a) != find(aresta.b))
{
join(aresta.a, aresta.b);
mst.push_back(aresta);
}
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
cin >> a >> b >> w;
Aresta adj(a, b, w);
pq.push(adj);
}
kruskal();
// imprimir a MST
for(int i = 1;i < n;i++) cout << mst[i].a << " " << mst[i].b << " " << mst[i].w << "\n";
return 0;
}