-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimsAlgo.cpp
79 lines (74 loc) · 1.37 KB
/
primsAlgo.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
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define vi std::vector<int>
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define si set<int>
#define mii map<int, int>
using namespace std;
class elem
{
public:
int par;
int key;
int val;
elem(int a, int b, int c)
{
par = a;
key = b;
val = c;
}
};
bool operator<(const elem &lhs, const elem &rhs)
{
return lhs.val > rhs.val;
}
vector<pii> g[100];
int n, m;
int cov[100];
int num;
priority_queue< elem, vector<elem> > p;
void PrimMST()
{
cin >> n >> m;
rep(i,0,m)
{
int a, b, c;
cin >> a >> b >> c;
g[a].push_back(make_pair(b, c));
g[b].push_back(make_pair(a, c));
}
p.push(elem(0, 1, 0));
while(num < n)
{
elem e = p.top();
p.pop();
if(cov[e.key] == 1)
continue;
++num;
cov[e.key] = 1;
int par = e.par;
int nod = e.key;
int val = e.val;
if(num > 1)
cout << e.par << " " << e.key << endl;
rep(i,0,g[nod].size())
{
pii a = g[nod][i];
p.push(elem(e.key, a.F, e.val+a.S));
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
rep(j,1,t+1){
PrimMST();
}
}