forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
114 lines (90 loc) · 2.19 KB
/
Dijkstra.cpp
File metadata and controls
114 lines (90 loc) · 2.19 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Created by ButcherX on 23-11-15.
//
#include"../header/heapEnhanced.h"
#include"../header/graph.h"
struct wrap
{
graphNode* node;
int dis;
wrap(graphNode* _node, int _dis):node(_node), dis(_dis){}
};
struct cmptor
{
bool operator()(const wrap* a, const wrap* b) {
return a->dis > b->dis;//小顶堆
}
};
void addOrUpdateOrIgnore(heapGreater<wrap*, cmptor> &heap, unordered_map<graphNode*, wrap*> &help, graphNode* node, int dis)
{
if(help.find(node) == help.end())
{
wrap* wp = new wrap(node, dis);
heap.push(wp);
help.emplace(node, wp);
}
else
{
wrap* wp = help.find(node)->second;
if(wp->dis > dis)
{
wp->dis = dis;
heap.modify(wp, wp);
}
}
}
unordered_map<graphNode*, int> disMap(graphNode* node)
{
unordered_map<graphNode*, int> retMap;
unordered_map<graphNode*, wrap*> help;
heapGreater<wrap*, cmptor> heap;
wrap* nd = new wrap(node, 0);
heap.push(nd);
help.emplace(node, nd);
while(!heap.empty())
{
nd = heap.pop();
retMap.emplace(nd->node, nd->dis);
for(auto edge : nd->node->edges)
{
addOrUpdateOrIgnore(heap, help, edge->to, nd->dis + edge->weight);
}
}
for(auto n : help)
delete n.second;
return retMap;
}
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 startNode =gra.nodes['a'];
auto map = disMap(startNode);
char ch;
for(auto n : map)
{
ch = n.first->value;
std::cout << "node: " << ch << " distance: " << n.second << std::endl;
}
}