-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim'sAlgo.cpp
54 lines (44 loc) · 1.18 KB
/
Prim'sAlgo.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
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int>Pair;
vector<Pair>adj[100000];
int n;
void primMST() {
priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
vector<int> key(n, INT_MAX);
vector<int> parent(n, -1);
vector<bool> inMST(n, false);
pq.push(make_pair(0, 0));
key[0] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
inMST[u] = true;
for (auto x : adj[u]) {
int v = x.first;
int weight = x.second;
if (!inMST[v] && key[v] > weight) {
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
cout << "Edge \tWeight\n";
for (int i = 1; i < n; ++i)
cout << parent[i] << " - " << i << "\t" << key[i] << "\n";
}
int main() {
int e,x,y,w;
cin>>n>>e;
while(e--){
cin>>x>>y>>w;
adj[x].push_back({y,w});
adj[y].push_back({x,w});
}
// primMST();
for(auto child:adj[0]){
cout<<child.first<<" "<<child.second<<endl;
}
return 0;
}