-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpanningTree_Prims.cpp
99 lines (75 loc) · 1.47 KB
/
SpanningTree_Prims.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#include <unordered_set>
using namespace std;
struct Edge {
int X;
int Y;
int Weight;
Edge(int x, int y, int w) {
X = x;
Y = y;
Weight = w;
}
};
void g_add(map<int, vector<Edge>>& g, int x, int y, int r) {
if (g.find(x) == g.end())
g.insert({ x, vector<Edge>() });
g[x].push_back(Edge(x, y, r));
}
map<int, vector<Edge>> parseData() {
int n; //# of nodes
int m; //# of edges
int x; //endpoint 1
int y; //endpont 2
int r; //weight
cin >> n >> m;
map<int, vector<Edge>> g;
for (int i = 0; i < m; i++)
{
cin >> x >> y >> r;
g_add(g, x, y, r);
g_add(g, y, x, r);
}
return g;
}
Edge getCheapest(map<int, vector<Edge>>& g, unordered_set<int>& x) {
map<int, vector<Edge>> vertexes;
Edge min(0, 0, 9999999999);
for (auto item : x) {
for (auto e : g[item]) {
if (min.Weight > e.Weight && x.find(e.Y) == x.end()) {
min = e;
}
}
}
return min;
}
int prims(map<int, vector<Edge>>& g, int start) {
vector<int> nodes; for (auto it : g) nodes.push_back(it.first);
unordered_set<int> x;
vector<Edge> t;
x.insert(start);
while (x.size() != nodes.size()) {
auto e = getCheapest(g, x);
t.push_back(e);
x.insert(e.Y);
}
int ret = 0;
for (auto it : t)
ret += it.Weight;
return ret;
}
int main() {
auto g = parseData();
int start;
cin >> start;
auto ret = prims(g, start);
cout << ret;
}