forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
78 lines (77 loc) · 1.91 KB
/
prim.cpp
File metadata and controls
78 lines (77 loc) · 1.91 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
70
71
72
73
74
75
76
77
78
//
// Created by ButcherX on 23-11-14.
//
#include"../header/graph.h"
#include<iostream>
#include<queue>
#include<list>
#include<unordered_set>
using std::priority_queue;
using std::list;
using std::unordered_set;
struct cmp
{
bool operator()(graphEdge* a, graphEdge* b)
{
return a->weight > b->weight;
}
};
list<graphEdge*> prim(graph _graph)
{
unordered_set<graphNode*> unlockedNodes;
priority_queue<graphEdge*, vector<graphEdge*>, cmp> unlockedEdges;
list<graphEdge*> ret;
graphNode* node;
graphEdge* edge;
for(auto n : _graph.nodes)
{
node = n.second;
if(unlockedNodes.find(node) != unlockedNodes.end())
continue;
unlockedNodes.emplace(node);
for(auto e : node->edges)
unlockedEdges.push(e);
while(!unlockedEdges.empty())
{
edge = unlockedEdges.top();
unlockedEdges.pop();
if(unlockedNodes.find(edge->to) == unlockedNodes.end())
{
unlockedNodes.emplace(edge->to);
ret.push_back(edge);
for(auto e : edge->to->edges)
unlockedEdges.push(e);
}
}
}
return ret;
}
int main()
{
vector<vector<int>> vec = {
{3, 'a', 'b'},
{3, 'b', 'a'},
{1, 'a', 'c'},
{1, 'c', 'a'},
{1, 'b', 'c'},
{1, 'c', 'b'},
{10, 'b', 'e'},
{10, 'e', 'b'},
{2, 'c', 'e'},
{2, 'e', 'c'},
{50, 'c', 'f'},
{50, 'f', 'c'},
{1, 'e', 'g'},
{1, 'g', 'e'},
{6, 'f', 'h'},
{6, 'h', 'f'},
{3, 'f', 'g'},
{3, 'g', 'f'},
{9, 'g', 'h'},
{9, 'h', 'g'}
};
graph gra = graphAdaptor(vec);
auto ret = prim(gra);
for(auto e : ret)
std::cout << e->weight << std::endl;
}