forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphPrimsAlgorithm.cpp
59 lines (45 loc) · 1.22 KB
/
graphPrimsAlgorithm.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
#include <bits/stdc++.h>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<pair<int, int>> adj[n];
int a, b, wt;
for(int i = 0; i < n; i++) {
cin >> a >> b >> wt;
adj[a].push_back(make_pair(b, wt));
adj[b].push_back(make_pair(a, wt));
}
int key[n];
bool mst[n];
int parent[n];
for(int i = 0; i < n; i++) {
key[i] = INT_MAX, mst[i] = false, parent[i] = -1;
}
key[0] = 0;
parent[0] = -1;
for(int count = 0; count < n-1; count++) {
int mini = INT_MAX, u;
for(int v = 0; v < n; v++) {
if(mst[v] == false && key[v] < mini){
mini = key[v], u = v;
}
}
mst[u]= true;
for(auto it: adj[u]) {
int v = it.first;
int weight = it.second;
if(mst[v] == false && weight < key[v]) {
key[v] = weight, parent[v] = u;
}
}
}
for(int i = 0; i < n; i++) {
cout << parent[i] << "-" << i << "\n";
}
return 0;
}