-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprim_mst.cpp
105 lines (82 loc) · 2.18 KB
/
prim_mst.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
using namespace std;
struct Edge{
int dest;
double weight;
Edge(){}
Edge(int dest, double weight){
this->dest = dest;
this->weight = weight;
}
};
struct Vertex {
vector<Edge> edges;
};
struct Compare{
bool operator ()( Edge & e1, Edge & e2){
if(e1.weight < e2.weight)
return true;
if(e1.weight > e2.weight)
return false;
return false;
}
};
class Graph{
public:
vector<Vertex> adj_list;
int vertex_num;
Graph(int vertex_num){
this->vertex_num = vertex_num;
for(int i = 0; i < vertex_num; i++)
adj_list.push_back(Vertex());
}
double primMST(){
priority_queue<Edge, vector<Edge>, Compare> p_que;
Edge e;
double weights = 1;
bool * visited = new bool[vertex_num];
for(int i = 0; i < vertex_num; i++){
visited[i] = false;
}
int v = 0;
visited[v] = true;
for(int i = 1; i < vertex_num; i++){
for(int j = 0; j < adj_list[v].edges.size(); j++){
e = adj_list[v].edges[j];
if(!visited[e.dest]){
p_que.push(e);
}
}
do{
e = p_que.top();
p_que.pop();
}
while(visited[e.dest]);
weights = weights * e.weight;
visited[e.dest] = true;
v = e.dest;
}
delete [] visited;
visited = NULL;
return weights;
}
};
int main(){
std::ios_base::sync_with_stdio(false);
int vertex_num, edges_num, v1, v2;
double weight;
cin >> vertex_num >> edges_num;
Graph g(vertex_num);
for(int i = 0; i < edges_num; i++){
cin >> v1 >> v2 >> weight;
Edge e1(v1, weight);
g.adj_list[v2].edges.push_back(e1);
Edge e2(v2, weight);
g.adj_list[v1].edges.push_back(e2);
}
cout << setprecision(3) << fixed << g.primMST() << endl;
return 0;
}