-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.cpp
50 lines (39 loc) · 1.26 KB
/
Heap.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
#include "Heap.h"
HeapPdi::HeapPdi() {
}
HeapPdi::~HeapPdi() {
}
void HeapPdi::push_max(long cost, int val) {
heap.push_back( std::pair<long, int>(cost, val) );
push_heap(heap.begin(), heap.end());
}
int HeapPdi::pop_max() {
make_heap(heap.begin(), heap.end());
std::pair<long, int> pair = heap.front();
pop_heap(heap.begin(), heap.end());
heap.pop_back();
return pair.second;
}
void HeapPdi::push_min(long cost, int val) {
heap.push_back( std::pair<long, int>(cost, val) );
push_heap(heap.begin(), heap.end(), std::greater< std::pair<long, int> >());
}
int HeapPdi::pop_min() {
make_heap(heap.begin(), heap.end(), std::greater< std::pair<long, int> >());
std::pair<long, int> pair = heap.front();
pop_heap(heap.begin(), heap.end(), std::greater< std::pair<long, int> >());
heap.pop_back();
return pair.second;
}
std::pair<long, int> HeapPdi::front_max() {
make_heap(heap.begin(), heap.end());
return heap.front();
}
std::pair<long, int> HeapPdi::front_min() {
make_heap(heap.begin(), heap.end(), std::greater< std::pair<long, int> >());
return heap.front();
}
void HeapPdi::setHeap(std::vector< std::pair<long, int> > aHeap) {
std::vector< std::pair<long, int> >(heap).swap(heap);
heap = aHeap;
}