• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ push_heap函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中push_heap函数的典型用法代码示例。如果您正苦于以下问题:C++ push_heap函数的具体用法?C++ push_heap怎么用?C++ push_heap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了push_heap函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: dijkstra

void dijkstra (graph_t *g, int a, int b) {
    int i, j;
    a = a - 'a';
    b = b - 'a';
    for (i = 0; i < g->vertices_len; i++) {
        vertex_t *v = g->vertices[i];
        v->dist = INT_MAX;
        v->prev = 0;
        v->visited = 0;
    }
    vertex_t *v = g->vertices[a];
    v->dist = 0;
    heap_t *h = create_heap(g->vertices_len);
    push_heap(h, a, v->dist);
    while (h->len) {
        i = pop_heap(h);
        if (i == b)
            break;
        v = g->vertices[i];
        v->visited = 1;
        for (j = 0; j < v->edges_len; j++) {
            edge_t *e = v->edges[j];
            vertex_t *u = g->vertices[e->vertex];
            if (!u->visited && v->dist + e->weight <= u->dist) {
                u->prev = i;
                u->dist = v->dist + e->weight;
                push_heap(h, e->vertex, u->dist);
            }
        }
    }
}
开发者ID:zorgnax,项目名称:rosettacode,代码行数:31,代码来源:dijkstra.c


示例2: solve

int solve(const IntVec& A, const IntVec& B, int k)
{
    IntVec q(k);
    int n = 0;
    int NA = A.size() >= k ? k : A.size();
    int NB = B.size() >= k ? k : B.size();

    for (int i = 0; i < NA; ++i) {
        for (int j = 0; j < NB; ++j) {
            int s = A[i] + B[i];
            if (n == k) {
                if (s < q[0]) {
                    pop_heap(q.begin(), q.end());
                    q[n - 1] = A[i] + B[j];
                    push_heap(q.begin(), q.begin() + n);
                }
            } else {
                q[n++] = A[i] + B[j];
                push_heap(q.begin(), q.begin() + n);
            }
            //			cout << n << "," << q << endl;
        }
    }
    //	cout << q << endl;
    return q[0];
}
开发者ID:adenzhang,项目名称:algotests,代码行数:26,代码来源:KthSmallestSumFromSortedArrays.cpp


示例3: mergeKLists

 ListNode* mergeKLists(vector<ListNode*>& lists) {        
     vector<ListNode *> minHeap;
     for (auto list: lists) {
         if (list != nullptr) {
             minHeap.push_back(list);
         }
     }
     make_heap(minHeap.begin(), minHeap.end(), compare);
     
     ListNode *res = new ListNode(-1);
     ListNode *cur = res;
     
     while (!minHeap.empty()) {
         pop_heap(minHeap.begin(), minHeap.end(), compare);
         ListNode *node = minHeap.back();
         minHeap.pop_back();
         
         cur->next = node;
         cur = cur->next;
         
         
         if (node->next) {
             minHeap.push_back(node->next);
             push_heap(minHeap.begin(), minHeap.end(), compare);
         }
     }
     
     return res->next;
 }
开发者ID:longjianjiang,项目名称:DailyAlgorithm,代码行数:29,代码来源:23_merge_k_sorted_lists.cpp


示例4: getNewsFeed

  /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item
   * in the news feed must be posted by users who the user followed or by the
   * user herself. Tweets must be ordered from most recent to least recent. */
  vector<int> getNewsFeed(int userId) {
    vector<pair<Tweet *, Tweet *>> h;
    for (const int &u : following[userId]) {
      vector<Tweet> &t = tweets[u];
      if (t.size() > 0) {
        h.emplace_back(t.data(), t.data() + t.size() - 1);
      }
    }

    auto &t = tweets[userId];
    if (t.size() > 0) {
      h.emplace_back(t.data(), t.data() + t.size() - 1);
    }
    auto f = [](const pair<Tweet *, Tweet *> &x,
                const pair<Tweet *, Tweet *> &y) {
      return x.second->time > y.second->time;
    };
    make_heap(h.begin(), h.end(), f);
    const int nums = 10;
    vector<int> result;
    result.reserve(nums);
    for (int i = 0; i < nums && !h.empty(); ++i) {
      pop_heap(h.begin(), h.end(), f);
      pair<Tweet *, Tweet *> &next = h.back();
      result.push_back(next.second->id);
      if (next.first == next.second--)
        h.pop_back();
      else
        push_heap(h.begin(), h.end(), f);
    }
    return result;
  }
开发者ID:irmowan,项目名称:LeetCode,代码行数:35,代码来源:Design-Twitter.cpp


示例5: ASSERT

void TimerBase::heapDecreaseKey()
{
    ASSERT(m_nextFireTime != 0);
    checkHeapIndex();
    push_heap(TimerHeapIterator(0), TimerHeapIterator(m_heapIndex + 1));
    checkHeapIndex();
}
开发者ID:flwh,项目名称:Alcatel_OT_985_kernel,代码行数:7,代码来源:Timer.cpp


示例6: memset

bool Graph::pfs()
{
    int v, j, ww, x;
    bool caled[MaxVertex];
    int wt[MaxVertex];
    vector<Node> myheap;
    memset(father, 0xff, sizeof(int)*V);
    memset(caled, 0, sizeof(bool)*V);
    memset(wt, 0xff, sizeof(int)*V);
    myheap.clear();
    father[s] = s;
    myheap.push_back(Node(s, INT_MAX));
    // assert(s != t);
    while(!myheap.empty())
    {
        v = myheap[0].v;
        ww = myheap[0].wt;
        pop_heap(myheap.begin(), myheap.end(), cmp);
        myheap.pop_back();
        if(caled[v]) continue;
        caled[v] = 1;
        for(j = 0; j < V; ++j)
            if(cap[v][j]>0 && !caled[j])
            {
                x = ww<cap[v][j] ? ww : cap[v][j];
                if(x <= wt[j]) continue;
                father[j] = v;
                if(j == t) return 1;
                wt[j] = x;
                myheap.push_back(Node(j, x));
                push_heap(myheap.begin(), myheap.end(), cmp);
            }
    }
    return 0;
}
开发者ID:VarickQ,项目名称:ACM,代码行数:35,代码来源:Ford-Fulkson最大流算法(最大容量增广路径实现).cpp


示例7: make_heap

 ListNode *mergeKLists(vector<ListNode *> &lists) {
     if(lists.size() == 0) {
         return NULL;
     }
     vector<ListNode *> nodeHeap;
     for(int i = 0; i < lists.size(); i++) {
         // only push non-NULL node into vector
         if(lists[i] != NULL) {
             nodeHeap.push_back(lists[i]);
         }
     }
     make_heap(nodeHeap.begin(), nodeHeap.end(), cmpNode);
     ListNode *fakeHead = new ListNode(INT_MIN), *curr = fakeHead;
     while(!nodeHeap.empty()) {
         ListNode *tmp = nodeHeap.front();
         curr->next = tmp;
         pop_heap(nodeHeap.begin(), nodeHeap.end(), cmpNode);
         // for the vector, also need to pop back
         // the poped elememt will move to the end of the vector
         nodeHeap.pop_back();
         curr = curr->next;
         // only push non-NULL node into heap
         if(tmp->next != NULL) {
             nodeHeap.push_back(tmp->next);
             push_heap(nodeHeap.begin(), nodeHeap.end(), cmpNode);
         }
     }
     return fakeHead->next;
 }
开发者ID:starcroce,项目名称:leetcode,代码行数:29,代码来源:merge_k_sorted_lists.cpp


示例8: pop_heap

void pop_heap(void *base, size_t n, size_t size,
               int (*cmp)(const void *, const void *)) {
  size_t hole = 1;
  if (n == 0)
    return;

  memswap(base, (char *)base + (n-1) * size, size);

  while (1) {
    size_t child1 = hole * 2;
    size_t child2 = hole * 2 + 1;

    if (child1 >= n)
      break;
    else if (child2 < n)
      if ((*cmp)((const char *)base + (child1-1) * size,
		 (const char *)base + (child2-1) * size) < 0)
	child1 = child2;
    memswap((char *)base + (hole-1) * size,
	    (char *)base + (child1-1) * size, size);
    hole = child1;
  }

  push_heap(base, hole, size, cmp);
}
开发者ID:NeonTheBlackstar,项目名称:RiboDatabase,代码行数:25,代码来源:glam2_heap.c


示例9: main

int main () {
  unsigned int n, i;
  unsigned int x;
  double med;

  int v[250000/2+3];

  i = 0;

  cin >> n;
   for(; i < n/2+1; ++i) cin >> v[i];
   make_heap(v,v+n/2+1);
   for(; i < n; ++i){
     cin >> v[n/2+1];
     push_heap(v,v+n/2+2);
     pop_heap(v,v+n/2+2);
   }
    

  if (n % 2 != 0)
    med = v.back();
  else {
    x = v.back();
    v.pop_back();
    med = (v.back()*0.5 + x*0.5);
  }
  cout << setiosflags (ios::fixed) << setprecision(1) << med << endl;   

  return 0;
}
开发者ID:shayenne,项目名称:desafios2015,代码行数:30,代码来源:Py22.cpp


示例10: make_heap

	ListNode *mergeKLists(vector<ListNode *> &lists) {
		// wyuan; 10/11/2014; Maitain a min heap. Use make_heap in <algorithm> 
		if(lists.empty())
		    return NULL;
		int size = lists.size();
		// vector<node> heap(size);  Which one is better?
		vector<node> heap;
		heap.reserve(size);
		for(int i = 0; i < size; i++)
			heap.push_back(node(lists[i]));
		make_heap(heap.begin(), heap.end(), greater<node>());
		ListNode *head = new ListNode(0); // Init!!!!!!
		ListNode *pre = head;

		pop_heap(heap.begin(), heap.end(), greater<node>());
		node minNode = heap.back();
		heap.pop_back();

		while(minNode.val != INT_MAX)
		{
			pre->next = minNode.from;
			pre = pre->next;
			ListNode *next = minNode.from->next;
			heap.push_back(node(next));
			push_heap(heap.begin(), heap.end(), greater<node>());			
			pop_heap(heap.begin(), heap.end(), greater<node>());
			minNode = heap.back();
			heap.pop_back();
		}


		ListNode *ret = head->next;
		delete head;
		return ret;
	}
开发者ID:wyuan1704,项目名称:leetcode,代码行数:35,代码来源:Merge_k_Sorted_Lists.cpp


示例11: pop_heap

void pop_heap(int a[], int e)
{
  int p;
  int end;
  int h = 1;

  // 最大元素放入顺序位置
  end = a[h-1];
  
  // 回溯到叶子节点
  while ((p = h * 2) < e)
    {
      if (p+1 > e)
        {
          a[h-1] = a[p-1];
          break;
        }
      if (a[p-1] > a[p])
        {
          a[h-1] = a[p-1];
          h = p;
        }
      else
        {
          a[h-1] = a[p];
          h = p+1;
        }
    }
  // 填充空位
  a[h-1] = a[e-1];
  //上溯
  push_heap(a, p/2);
  a[e-1] = end;
}
开发者ID:errord,项目名称:sirius,代码行数:34,代码来源:sort.c


示例12: calculateKey

void GridWorld::updateVertex(GridWorld::Tile*& tile){
	bool isIncosistent = tile->rhs != tile->g; //potential problem with floating point comparison?

	if (isIncosistent && tile->isOpen){
		//tile->h = calculateH(tile);
		tile->key = calculateKey(tile);
		make_heap(open.begin(), open.end(), GridWorld::compareTiles);

	}
	else if (isIncosistent && !tile->isOpen){
		//tile->h = calculateH(tile);
		tile->key = calculateKey(tile);
		tile->isOpen = true;

		open.push_back(tile);
		push_heap(open.begin(), open.end(), GridWorld::compareTiles);

	}
	else if (!isIncosistent && tile->isOpen){
		open.erase(std::find(open.begin(), open.end(), tile));
		make_heap(open.begin(), open.end(), GridWorld::compareTiles);
		tile->isOpen = false;
	}

}
开发者ID:UBC-Snowbots,项目名称:IGVC2015,代码行数:25,代码来源:GridWorld.cpp


示例13: new_walker

whp new_walker(pp_pp* pp, mpz_t limit, int invsum) {
	whp wh;
	walker* w;
	walk_result* wr;
	int numsize = pp->valnumsize;

	wh = arena_size;
	w = WP(wh);
	arena_size += walker_charsize(numsize);
	grow_arena(w, arena_size);
	w->heap = mbh_new(I2P(wh), &mbh_compare_wr);
	w->pp = pp;
	w->numsize = numsize;
	w->adder = pp->adder;
	w->cmper = pp->cmper;
	w->invsum = invsum;
	w->vecsize = (pp->valsize + 31) >> 5;
	w->arenanext = (wrhp)0;
	w->have_previous = 0;
	mpx_set_z(w_limit(w), numsize, limit);

	w_pick_arena(w, wr);
	wr->invsum = 0;
	wr->nextbit = pp->valsize;
	mpx_set_ui(wr_next_discard(w, wr), numsize, 0);
	mpx_set_ui(wr_discard_direct(w, wr), numsize, 0);
	memset(wr_vec_direct(w, wr), 0, w->vecsize * sizeof(int));
	push_heap(w, wr);

	return wh;
}
开发者ID:hvds,项目名称:seq,代码行数:31,代码来源:walker.c


示例14: make_pair

Matrix *merge( Entry* buf, int *counts, int *offsets, int P, std::function<bool(const pair<int,Entry>, const pair<int,Entry>)> reverseSort ) {
  vector<pair<int,Entry> > next;
  int is[P];
  for( int i = 0; i < P; i++ )
    is[i] = 0;
  // initial population of the heap
  for( int i = 0; i < P; i++ )
    if( counts[i] > is[i] ) {
      next.push_back( make_pair( i,buf[offsets[i]+is[i]] ) );
      is[i]++;
    }
  make_heap(next.begin(), next.end(), reverseSort );

  Matrix* ret = new Matrix;
  // put the first entry in the matrix
  if( !next.empty() ) {
    pop_heap(next.begin(), next.end(), reverseSort );
    ret->push_back( next.back().second );
    int i = next.back().first;
    if( counts[i] > is[i] ) {
      next.back().second = buf[offsets[i]+is[i]];
      push_heap(next.begin(), next.end(), reverseSort );
      is[i]++;
    } else {
      next.pop_back();
    }
  }
    
  while( !next.empty() ) {
    pop_heap(next.begin(), next.end(), reverseSort );
    if( ret->back().first.first == next.back().second.first.first &&
	ret->back().first.second == next.back().second.first.second ) {
      ret->back().second += next.back().second.second;
    } else {
      ret->push_back( next.back().second );
    }
    int i = next.back().first;
    if( counts[i] > is[i] ) {
      next.back().second = buf[offsets[i]+is[i]];
      push_heap(next.begin(), next.end(), reverseSort );
      is[i]++;
    } else {
      next.pop_back();
    }
  }
  return ret;
}
开发者ID:benjamingr,项目名称:CAPS,代码行数:47,代码来源:merge.cpp


示例15: while

void landmarked_neighbors<Tdata,Tint>::advance_heap()
{
  int thisGroup,pointIndex,thisPointIndex;
  const Tdata *thisPoint;
  Tdata tmpR2;

  if (!use_landmarks) {
    return;
  }

  // Add points to heap (if necessary).  Here's the scheme &
  // considerations:
  // (1) add all groups that have a lower-bound distance
  //     within the currentR2
  // (2) if the closest point yet has a square distance larger than
  //     currentR2, it might not be the closest point: there might be
  //     another landmark group containing a closer point.  So we increase
  //     currentR2 and add all the groups up to that distance.
  //
  // This leads to a double-while construction.  We could do it with a
  // single-while, but it could result in unnecessary points being
  // added to the heap, if a later group already scheduled for
  // addition has the closest point.
  //mexPrintf("1: currentR2 %g, lmIterator->R2 %g\n",currentR2,lmIterator->R2);
  while (lmIterator < landmarks_sdi.end() && lmIterator->R2 <= currentR2) {
    //mexPrintf("2: currentR2 %g, lmIterator->R2 %g\n",currentR2,lmIterator->R2);
    while (lmIterator < landmarks_sdi.end() && lmIterator->R2 <= currentR2) {
      // Add all the points in the current group
      thisGroup = lmIterator->index;
      //mexPrintf("Adding group %d\n",thisGroup);
      for (pointIndex = 0; pointIndex < lminfo.n_landmarkList[thisGroup]; pointIndex++) {
	thisPointIndex = (int) lminfo.landmarkList[thisGroup][pointIndex] - lminfo.index_offset;
	thisPoint = x + lminfo.d*thisPointIndex;
	tmpR2 = sqrdist(&(*y.begin()),&(*y.end()),thisPoint);
	*point_heap_end = SqrdistIndex<Tdata>(tmpR2,thisPointIndex);
	// make it so the top of the heap is the closest point
	push_heap(point_heap.begin(),++point_heap_end,is_farther<Tdata>());
      }
      //mexPrintf("Heap status:\n");
      //typename vector< SqrdistIndex<Tdata> >::iterator phI;
      //for (phI = point_heap.begin(); phI < point_heap_end; phI++)
      //  mexPrintf("x%d (%g) ",phI->index,phI->R2);
      //mexPrintf("\n");
      lmIterator++;
    } // end of inside while loop
    // We want to make sure we keep including groups until we get some
    // points on the heap that are closer than any points not on the
    // heap.  The way to do that is to adjust currentR2 to reflect the
    // distance to the closest point.
    if (!is_empty())
      currentR2 = point_heap.begin()->R2;
    else if (lmIterator < landmarks_sdi.end()) {
      // If the heap is still empty, but we haven't exhausted all
      // landmarks, then we must have had only empty landmark
      // groups. In that case, increment to the next landmarkR2.
      currentR2 = lmIterator->R2;
    }
  }
}
开发者ID:drevond,项目名称:spikeSorting,代码行数:59,代码来源:landmarked_neighbors.cpp


示例16: ASSERT

void TimerBase::heapDecreaseKey()
{
    ASSERT(m_nextFireTime != 0);
    checkHeapIndex();
    TimerBase** heapData = timerHeap().data();
    push_heap(TimerHeapIterator(heapData), TimerHeapIterator(heapData + m_heapIndex + 1), TimerHeapLessThanFunction());
    checkHeapIndex();
}
开发者ID:1833183060,项目名称:wke,代码行数:8,代码来源:Timer.cpp


示例17: AddTimerWithType

void Timer :: AddTimerWithType(const uint64_t llAbsTime, const int iType, uint32_t & iTimerID)
{
    iTimerID = m_iNowTimerID++;

    TimerObj tObj(iTimerID, llAbsTime, iType);
    m_vecTimerHeap.push_back(tObj);
    push_heap(begin(m_vecTimerHeap), end(m_vecTimerHeap));
}
开发者ID:LngMH,项目名称:phxpaxos,代码行数:8,代码来源:timer.cpp


示例18: add

 int add(int val) {
     std::vector<int>& minHeap = *ptMinHeap;
     if (minHeap.size() < _k) {
         minHeap.push_back(val);
         push_heap(minHeap.begin(), minHeap.end(), std::greater<int>());
     } else if (val > minHeap[0]) {
         minHeap[0] = val;
         std::make_heap(minHeap.begin(), minHeap.end(), std::greater<int>());
     }
     return minHeap[0];
 }
开发者ID:Finalcheat,项目名称:leetcode,代码行数:11,代码来源:Kth-Largest-Element-in-a-Stream.cpp


示例19: max_heap_sort

void max_heap_sort(int a[], int l)
{
  int i;

  // 构造heap
  for (i = 1; i <= l; i++)
    push_heap(a, i);
  // 序列化
  for (i = l; i > 0; i--)
    pop_heap(a, i);
}
开发者ID:errord,项目名称:sirius,代码行数:11,代码来源:sort.c


示例20: assert

void ParticleStorage::deleteParticle(Particle *p)
{
    assert(particles != NULL && p != NULL && available < capacity);
    Particle *q = freeAndAllocatedParticles[available];
    int index = *((int*) (((unsigned char*) p) + particleSize - sizeof(int)));
    *((int*) (((unsigned char*) q) + particleSize - sizeof(int))) = index;
    freeAndAllocatedParticles[index] = q;
    freeAndAllocatedParticles[available++] = p;
    if (pack) {
        push_heap(freeAndAllocatedParticles.begin(), freeAndAllocatedParticles.begin() + available, greater<Particle*>());
    }
}
开发者ID:paladin74,项目名称:proland,代码行数:12,代码来源:ParticleStorage.cpp



注:本文中的push_heap函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ push_instr函数代码示例发布时间:2022-05-30
下一篇:
C++ push_captures函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap