-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.cpp
More file actions
69 lines (58 loc) · 1.51 KB
/
prims.cpp
File metadata and controls
69 lines (58 loc) · 1.51 KB
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
#include <bits/stdc++.h>
using namespace std;
int getMinVertex(bool* visited, int* weights, int v){
int minVertex = -1;
for(int i = 0; i < v; i++){
if(!visited[i] && (minVertex == -1 || weights[i] < weights[minVertex])){
minVertex = i;
}
}
return minVertex;
}
void prims(int** edges, int v){
int* parents = new int[v];
int* weights = new int[v];
bool* visited = new bool[v];
for(int i = 0; i < v; i++){
weights[i] = INT_MAX;
visited[i] = false;
}
parents[0] = -1;
weights[0] = 0;
for(int i = 0; i < v; i++){
int minVertex = getMinVertex(visited, weights, v);
for(int i = 0; i < v; i++){
if(edges[minVertex][j] != 0 && !visited[j]){
if(edges[minVertex][j] < weights[j]){
weights[j] = edges[minVertex][j];
parents[j] = minVertex;
}
}
}
}
for(int i = 0; i < v; i++){
if(parents[i] > i){
cout << i <<" "<< parents[i]<<endl;
}else{
cout << parents[i] << " "<< i<<endl;
}
}
}
int main(){
int v, e;
cin >> v >> e;
int** edges = new int*[v];
for(int i = 0; i < v; i++){
edges[i] = new int[v];
for(int j = 0; j < v; j++){
edges[i][j] = 0;
}
}
for(int i = 0; i < e; i++){
int f,s, weight;
cin >> f >> s >> weight;
edges[f][s] = weight;
edges[s][f] = weight;
}
prims(edges, v);
}