priority-queue

How to preallocate(reserve) a priority_queue<vector>?

若如初见. 提交于 2020-01-11 00:06:57
问题 How can I preallocate a std::priority_queue with a container of type std::vector ? std::priority_queue<unsigned char, std::vector<unsigned char>> pq; pq.c.reserve(1024); Does not compile because the underlying vector is a protected member. Is it possible to use the constructor of the priority_queue to wrap it around a pre-reserved vector? 回答1: Yes, there's a constructor for that. It's slightly tedious that you also have to specify a comparator: std::vector<unsigned char> container; container

F# priority queue

淺唱寂寞╮ 提交于 2020-01-10 08:49:48
问题 Does the F# library include a priority queue? Else can someone point to me an implementation of priority queue in F#? 回答1: There's an implementation of a binomial heap here which is a common data structure for implementing priority queues. 回答2: Take a look at http://lepensemoi.free.fr/index.php/tag/data-structure for a whole bunch of F# implementations of various data structures. 回答3: FSharpx.Collections includes a functional Heap collection https://github.com/fsharp/fsharpx/blob/master/src

How to update elements inside an std::priority_queue?

…衆ロ難τιáo~ 提交于 2020-01-07 06:43:59
问题 #include <iostream> #include <map> #include <string> #include <vector> #include <stack> #include <stdio.h> #include <list> #include <string.h> #include <queue> #include <algorithm> #define pb push_back using namespace std; typedef pair<int,int> ii; struct node{ int digit; }; class Compare{ public: bool operator()(node* a,node* b){ return (a->digit)>(b->digit); } }; int main() { priority_queue<node*,vector<node*>,Compare> pq; vector<node*> vec; node* p = new node(); node* q = new node(); node*

priority_queue declaration and bool operator < declaration

有些话、适合烂在心里 提交于 2020-01-07 05:04:08
问题 my problem is: I have my program with 2 class plus the main; I've declared a priority_queue inside a member function of a class; I have to define the comparison and I think the code I should use is: // Determine priority (in the priority queue) bool operator < (const node & a, const node & b) { return a.getPriority() > b.getPriority(); } Question: where Should I insert this piece of code? Could someone help me? thanks 回答1: It looks like your operator< is possibly a poor addition to node . Ask

Priority queue with two priority values

五迷三道 提交于 2020-01-05 12:10:56
问题 As it is good known, elements which are inserted to the priority queue have a value which determines its priority. For example if I have five elements A,B,C,D,E with priorities (let's call this priority values priorityI ): A = 10, B = 5, C = 1, D = 3, E = 2 . But how can I write a priority queue where I can define two priority values, I mean: if two elements has the same value of priorityI , then value priorityII decides which element should be taken first, like for example: element A has

Dijkstra's algorithm - priority queue issue

社会主义新天地 提交于 2020-01-05 08:37:11
问题 I have a Graph class with a bunch of nodes, edges, etc. and I'm trying to perform Dijkstra's algorithm. I start off adding all the nodes to a priority queue. Each node has a boolean flag for whether it is already 'known' or not, a reference to the node that comes before it, and an int dist field that stores its length from the source node. After adding all the nodes to the PQ and then flagging the source node appropriately, I've noticed that the wrong node is pulled off the PQ first. It

Prim's Algorithm

ε祈祈猫儿з 提交于 2020-01-04 15:27:48
问题 I am working on a minimum spanning tree using Prim's Algorithm with PriorityQueue in Java. However, I am getting the totalWeight (the minimum weight of the tree) wrong. Did I misunderstand the concept behind total weight, or is there some problem with my code? public int getMinSpanningTree(Graph g) { int[][] matrix = g.getEdgeMatrix(); int totalVertices = g.getNumberOfVertices(); boolean[] visit = new boolean[totalVertices]; int visitNum = 1; int totalWeight = 0; PriorityQueue<PriorityVertex>

Pass a custom comparer to a priority queue in Cython

Deadly 提交于 2020-01-04 08:05:12
问题 The Cython libcpp module contains a template for priority_queue , which is great, except for one thing: I cannot pass it a custom comparer (or, at least, I don't know how to). I need this because I need the priority_queue to do an argsort of sorts rather than a sort (yes, a priority queue is optimal for what I want to do), and I need it to be fast. Is this possible within Cython, perhaps by wrapping a queue in a custom way, or not at all? As an example, say I want to sort a vector[int[:]] by

Using Java PriorityQueue in Matlab

蹲街弑〆低调 提交于 2020-01-03 18:14:13
问题 I need a min-heap in Matlab and I'm trying to use Java's PriorityQueue. I'm stuck on how to supply the Comparator. So far I have initialized the PriorityQueue and can add one value-index pair to it: >> q = java.util.PriorityQueue q = [] >> q.add({1,3}) ans = 1 The problem occurs when I try to add more data: >> q.add({2,4}) ??? Java exception occurred: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.Comparable at java.util.PriorityQueue.siftUpComparable(Unknown

priority_queue with dynamic priorities

二次信任 提交于 2020-01-02 06:13:22
问题 I have a server application which accepts incomming queries and executes them. If there are too many queries they should be queued and if some of the other queries got executed the queued queries should be executed as well. Since I want to pass queries with different priorities I think using a priority_queue would be the best choice. e.g. The amout of the axcepting queries (a) hit the limt and new queries will be stored in the queue. All queries have a priority of 1 (lowest) if some of the