本文整理汇总了C++中PQ类的典型用法代码示例。如果您正苦于以下问题:C++ PQ类的具体用法?C++ PQ怎么用?C++ PQ使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PQ类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Node
VectorXf GeodesicDistance::compute(int nd) const {
PQ q;
q.push( Node( nd, 0 ) );
VectorXf d = 1e10*VectorXf::Ones( N_ );
updatePQ( d, q );
return d;
}
示例2: benchOpsThread
void benchOpsThread(bench_ops_thread_arg_t * arg)
{
wbmm_thread_init(arg->tid);
uint32_t seed1 = arg->tid;
uint32_t seed2 = seed1 + 1;
uint64_t ops = 0;
PQ * set = (PQ *)arg->set;
while (!bench_begin);
while (!bench_stop) {
int op = rand_r_32(&seed1) % 100;
int key = rand_r_32(&seed2) % KEY_RANGE;
if (op < 50) {
set->add(key);
}
else {
key = set->remove();
}
for (int i = 0; i < DELAY; i++) spin64();
ops++;
}
arg->ops = ops;
}
示例3: dijdij
int dijdij(int st,int ed){
v[0].reset(); v[1].reset();
for(int i=0;i<111;++i)d[i].clear();
d[st].pb(0);
PQ<pii,vector<pii>,greater<pii>> pq; pq.push({0,st});
while(pq.size()){
while(pq.size() && v[1][pq.top().Y])pq.pop();
if(pq.empty())break;
pii now=pq.top(); pq.pop();
int stp=v[0][now.Y]?1:0;
PDE3(now,stp,pq);
v[stp][now.Y]=1;
for(pii e:G[now.Y]){
d[e.X].pb(now.X+e.Y);
sort(d[e.X].begin(),d[e.X].end());
d[e.X].resize(unique(d[e.X].begin(),d[e.X].end())-d[e.X].begin());
if(d[e.X].size()>2u){
sort(d[e.X].begin(),d[e.X].end());
d[e.X].pop_back();
}
if(now.X+e.Y<=d[e.X].back()){
pq.push({now.X+e.Y,e.X});
}
}
}
if(d[ed].size()<2u)return -1;
else return d[ed][1];
}
示例4: test2
void test2() {
PQ p;
for (int i = 0;i < 10;i++) {
p.push(10-i);
}
while (p.empty() == false) {
cout << p.top() << endl;
p.pop();
}
}
示例5: sanityCheck
static bool sanityCheck()
{
PQ set;
uint32_t seed = 0;
for (uint32_t i = 0; i < INIT_SIZE; i++) {
int key = rand_r_32(&seed) % KEY_RANGE;
set.add(key);
}
bench_begin = false;
bench_stop = false;
thread * thrs[NUM_THREADS];
sanity_thread_arg_t args[NUM_THREADS];
for (uint32_t j = 0; j < NUM_THREADS; j++) {
sanity_thread_arg_t & arg = args[j];
arg.tid = j + 1;
arg.set = &set;
arg.ops = 0;
thrs[j] = new thread(sanityThread<PQ>, &arg);
}
// broadcast begin signal
bench_begin = true;
sleep(DURATION);
bench_stop = true;
for (uint32_t j = 0; j < NUM_THREADS; j++) thrs[j]->join();
uint32_t old = 0;
for (uint32_t i = 0; i < INIT_SIZE; i++) {
uint32_t num = set.remove();
if (old > num) {
cout << "error: heap invariant violated: "
<< "prev = " << old << " "
<< "curr = " << num << endl;
return false;
}
if (num == PQ_VAL_MAX) {
cout << "error: missing element(not linearizable)" << endl;
return false;
}
old = num;
}
uint32_t num = set.remove();
if (num != PQ_VAL_MAX) {
cout << "error: extra element(not linearizable)" << endl;
return false;
}
cout << "Sanity check: okay." << endl;
return true;
}
示例6: pq_delete_min
void
pq_delete_min(PQ pq, void *retval)
{
int floater; /* previous loser floating down */
int small_child; /* smaller child of floater */
assert(!pq_is_empty(pq));
/* first copy out the winner */
memcpy(retval, REF(pq, 0), pq->element_length);
--(pq->n);
if(pq_is_empty(pq)) {
/* pq empty, nothing to do */
return;
}
/* else */
memcpy(REF(pq, 0), REF(pq, pq->n), pq->element_length);
floater = 0;
for(;;) {
/* find smaller child of floater */
if(Child(floater, 0) >= pq->n) {
return; /* no children, bail out */
} else if(Child(floater, 1) >= pq->n) {
small_child = Child(floater, 0);
} else if(pq->compare(REF(pq, Child(floater, 0)), REF(pq, Child(floater, 1))) < 0) {
small_child = Child(floater, 0);
} else {
small_child = Child(floater, 1);
}
/* is floater <= small_child? */
if(pq->compare(REF(pq, floater), REF(pq, small_child)) <= 0) {
/* yes, we are done */
return;
} else {
/* no, swap and continue floating down */
pq_swap(pq, floater, small_child);
floater = small_child;
}
}
}
示例7: testHeap
void testHeap ( int n ) {
T* A = new T[2*n/3]; //创建容量为2*n/3的数组,并
for ( int i = 0; i < 2 * n / 3; i++ ) A[i] = dice ( ( T ) 3 * n ); //在其中随机生成2*n/3个词条
/*DSA*/printf ( "%d random keys created:\n", 2 * n / 3 );
/*DSA*/for ( int i = 0; i < 2 * n / 3; i++ ) print ( A[i] ); printf ( "\n" );
PQ heap ( A + n / 6, n / 3 ); //批量建堆(PQ_ComplHeap实现了Robert Floyd算法)
delete [] A;
/*DSA*/system("cls"); print ( heap ); Sleep(100);
while ( heap.size() < n ) { //随机测试
if ( dice ( 100 ) < 70 ) { //70%概率插入新词条
T e = dice ( ( T ) 3 * n ); /*DSA*/printf ( "Inserting" ); print ( e ); printf ( " ...\n" );
heap.insert ( e ); /*DSA*/printf ( "Insertion done\n" );
} else { //30%概率摘除最大词条
if ( !heap.empty() ) {
/*DSA*/printf ( "Deleting max ...\n" );
T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
}
}
/*DSA*/system("cls"); print ( heap ); Sleep(100);
}
while ( !heap.empty() ) { //清空
T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
/*DSA*/system("cls"); print ( heap ); Sleep(100);
}
}
示例8: runBench
static void runBench()
{
PQ set;
uint32_t seed = 0;
for (uint32_t i = 0; i < INIT_SIZE; i++) {
int key = rand_r_32(&seed) % KEY_RANGE;
set.add(key);
}
bench_begin = false;
bench_stop = false;
thread * thrs[NUM_THREADS];
bench_ops_thread_arg_t args[NUM_THREADS];
for (uint32_t j = 0; j < NUM_THREADS; j++) {
bench_ops_thread_arg_t & arg = args[j];
arg.tid = j + 1;
arg.set = &set;
arg.ops = 0;
thrs[j] = new thread(benchOpsThread<PQ>, &arg);
}
// broadcast begin signal
bench_begin = true;
sleep(DURATION);
bench_stop = true;
for (uint32_t j = 0; j < NUM_THREADS; j++)
thrs[j]->join();
uint64_t totalOps = 0;
for (uint32_t j = 0; j < NUM_THREADS; j++) {
totalOps += args[j].ops;
}
cout << ("Throughput(ops/ms): ")
<< std::setprecision(6)
<< (double)totalOps / DURATION / 1000 << endl;
}
示例9: HuffHuff
/**
*Requires that all Tree Nodes representing Active Characters have been created
*And Enqueued into the Priorty Queue
*Generates a huffman tree by combining the nodes in th PQ
*the root of the tree is at the head of the priorty queue
*/
void Huffman:: HuffHuff() //now it creates the huffman tree
{
if(!(q->getSize1())) //single element
q->enq(mTreeNodes(q->deq(),NULL));
while(q->getSize1())
{
q->enq(mTreeNodes(q->deq(),q->deq()));
}
cout<<"\n\n\t\tLegend Tree along with char at end\n"<<endl;
q->getHead()->displayTree();
system("pause");
}
示例10: insertNext
//Insert limit elements of the file fin into heap.
void insertNext(PQ & pq, ifstream & fin, int limit = 0)
{
if (limit == 0)
limit = numeric_limits<int>::max();
string word;
int ct;
while (!fin.eof() && pq.size < limit){
fin >> word >> ct;
pq.insert(ItemType(word, ct));
}
}
示例11: sanityThread
void sanityThread(sanity_thread_arg_t * arg)
{
wbmm_thread_init(arg->tid);
uint32_t seed1 = arg->tid;
uint32_t seed2 = seed1 + 1;
uint64_t ops = 0;
PQ * set = (PQ *)arg->set;
while (!bench_begin);
while (!bench_stop) {
int key = rand_r_32(&seed2) % KEY_RANGE;
set->add(key);
key = set->remove();
ops++;
}
arg->ops = ops;
}
示例12: main
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<vector<pii>> edges(n);
while(m--) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
edges[u].push_back({w, v});
edges[v].push_back({w, u});
}
PQ pq;
pq.push({0,0});
while(!pq.empty()) {
int dist = pq.top().first;
int curr = pq.top().second;
pq.pop();
if (vis[curr] != false) continue;
vis[curr] = true;
/*
* curr visited in "shortest path" order here
*/
for(pii it : edges[curr]) {
int ndist = it.first + dist;
int next = it.second;
pq.push({ndist, next});
}
}
return 0;
}
示例13: dijkstra
int dijkstra(int start ,int goal ,int maxRank)
{
memset(dis , 0x7F ,sizeof(dis));
memset(apr , 0 ,sizeof(apr));
dis [start] = 0;
que . size =0;
que . push(start);
while(!que . empty())
{
PQNode t;
bool notGet = true;
while(!que . empty())
{
t = que.top();
que.pop();
if(!apr[t . no])
{
notGet = false;
break;
}
}
if(notGet)break;
for(Node *p = adj[t . no];p ; p=p->next)
{
if( rank [p->y] > maxRank) continue;
if( rank [p->y] < maxRank - limRank) continue;
if(dis[p->y] > dis[t .no] + p->w)
{
dis[p->y] = dis[t .no] + p->w;
que .push(p->y);
}
}
apr [t. no] = true;
}
if( !apr[goal] ) return -1;
return dis [goal];
}
示例14: sanityCheckSequential
bool sanityCheckSequential()
{
const int max = 10000;
std::priority_queue<int32_t, vector<int32_t>, pqcompare> contrast;
PQ m;
uint32_t seed = 0;
for (int i = 0; i < max; i++) {
int32_t temp = rand_r_32(&seed) % KEY_RANGE;
contrast.push(temp);
m.add(temp);
}
for (int i = 0; i < max - 1; i++) {
uint32_t r1 = m.remove();
uint32_t r2 = contrast.empty() ? PQ_VAL_MAX : contrast.top();
if (!contrast.empty()) contrast.pop();
if (r1 != r2) {
cout << "different element at index " << i << ":"
<< r1 << " " << r2 << endl;
return false;
}
}
cout << "Sanity check: okay." << endl;
return true;
}
示例15: test1
void test1() {
PQ p;
p.push(1);
p.push(2);
p.push(3);
p.push(4);
p.push(5);
while (p.empty() == false) {
cout << p.top() << endl;
p.pop();
}
}