// 测试堆 void TestHeap() { Heap<int, greater<int>> heap; heap.Push(3); heap.Push(5); heap.Push(1); heap.Push(4); heap.Push(5); heap.Push(1); heap.Push(8); while (!heap.Empty()) { cout<<heap.Top()<<" "; heap.Pop(); } cout<<endl; //int array[10] = {9,1,3,5,6,7,8,0,2,4}; int array[10] = {10,16,18,12,11,13,15,17,14,19}; Heap<int> heap1(array, 10); while (!heap1.Empty()) { cout<<heap1.Top()<<" "; heap1.Pop(); } cout<<endl; }
void Dijkstra::Solve_distance() { weighted_vertex wt = {0,0,0}; heap.Push(wt); s1 = "0" + s1; s2 = "0" + s2; while (!heap.Empty()) { weighted_vertex top = heap.top(); vertex now_top = top.v; if (now_top.y + 1 < s2.length() && s1[now_top.x] != s2[now_top.y + 1]) { vertex temp = now_top; temp.y++; weighted_vertex wt; wt.w = Add_potential(now_top, temp, top.w + 1); wt.v = temp; heap.Push(wt); } if (now_top.x + 1 < s1.length() && s1[now_top.x + 1] != s2[now_top.y]) { vertex temp = now_top; temp.x++; weighted_vertex wt; wt.w = Add_potential(now_top, temp, top.w + 1); wt.v = temp; heap.Push(wt); } if (now_top.x + 1 < s1.length() && now_top.y + 1 < s2.length()) { int weight = 0; if (s1[now_top.x + 1] != s2[now_top.y + 1]) weight = 1; vertex temp = now_top; temp.x++; temp.y++; weighted_vertex wt; wt.w = Add_potential(now_top, temp, top.w + 1); wt.v = temp; heap.Push(wt); } if (now_top.x == (int)s1.length() - 1 && now_top.y == (int)s2.length() - 1) { vertex t = {0,0}; answer = Delete_potentials(t, now_top, top.w); break; } } }
void CreateHuffmanTree(const T* array, size_t size, const T& invalid) { struct Compare { bool operator()(HuffmanNode_P<T>*& lhs, HuffmanNode_P<T>*& rhs) { return lhs->_weight < rhs->_weight; } }; // 1.将所有值构建为节点放入到一个最小堆中,Compare仿函数做比较器 Heap<HuffmanNode_P<T>*, Compare> minHeap; for (int i = 0; i < size; ++i) { if (array[i] != invalid) { HuffmanNode_P<T>* node = new HuffmanNode_P<T>(array[i]); minHeap.Insert(node); } } if (minHeap.Empty()) return; // 2.获取出最小和次小的节点做孩子节点,并构建这两个孩子节点的父节点进行链接。 HuffmanNode_P<T>* parent = minHeap.GetHeapTop(); while (minHeap.Size() > 1) { HuffmanNode_P<T>* first = minHeap.GetHeapTop(); minHeap.Remove(); HuffmanNode_P<T>* second = minHeap.GetHeapTop(); minHeap.Remove(); parent = new HuffmanNode_P<T>(first->_weight + second->_weight); parent->_left = first; parent->_right = second; first->_parent = parent; second->_parent = parent; minHeap.Insert(parent); } _root = parent; }
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) { if (startFuel >= target) return 0; Heap bfs_q; bfs_q.Insert(Stop{-1, startFuel}); auto n = 0; Heap q; unordered_map<int, bool> is_added; while (!bfs_q.Empty()) { auto s = bfs_q.Pop(); for(auto i = s.index+1; i < stations.size(); ++i) { if (s.fuel >= stations[i][0]) { if (is_added.find(i) != is_added.end()) { //continue break; } is_added[i] = true; auto p = Stop{i, stations[i][1] + s.fuel}; if (p.fuel >= target) return n+1; q.Insert(p); } else { break; } } if (bfs_q.Empty() && !q.Empty()) { bfs_q = q; n++; q.Clear(); is_added.clear(); } } return -1; }