当前位置: 首页>>代码示例>>C++>>正文


C++ PriorityQueue::contains方法代码示例

本文整理汇总了C++中PriorityQueue::contains方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::contains方法的具体用法?C++ PriorityQueue::contains怎么用?C++ PriorityQueue::contains使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


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

示例1: main

int main(){
    
    PriorityQueue* pr = new PriorityQueue();

    pr->insertElem(4,13);
    pr->insertElem(3,5);
    pr->insertElem(2,9);
    pr->insertElem(1,2);
    
    for(int i=0 ; i<pr->size() ;i++){
        
        printf("%d\n", pr->minQueue[i]->priority  );

    }


    
    if( pr->contains(4) ) {
        pr->changePriority(4,1);
    }

    while(!pr->isEmpty()){
        Element * elem = pr->minPriority();
        printf("(%d,%d)\n",elem->vertex,elem->priority);
    }

}
开发者ID:rvshettigar,项目名称:graph,代码行数:27,代码来源:PrQueDriver.cpp

示例2: mst

void Prim::mst(int root,Color color){
	Q.clear();
	int size=G->V();  // number of vertices
	for(int i=0;i<size;i++){
		dist[i]=1000.0;
		edges[i]=NO_EDGE;
		if(G->get_node_value(i)==color){  // only choose the same color
		    	//dist[i]=INF;    // equal to infinite
			//cout<<"insert node!"<<i<<" and dist is "<<dist[i]<<endl;
		Q.Insert(Type_Queue_Element (i,dist[i]));  // assign V[G] to Q
		}
	}
	dist[root]=0.0;
	if(!Q.contains(root)) cout<<"not include root !!"<<endl;
	else {
		Q.chgPrioirity(root,dist[root]);   //dist[i] and priority value in priority queue must be synchronized
		edges[root]=ROOT_EDGE;
		while(!Q.empty()){
			Type_Queue_Element currElement=Q.top();
            Q.minPrioirty();  // remove from priority queue
			int currNode=currElement.first;
			if(edges[currNode]!=NO_EDGE){
				dist[currNode]=currElement.second;
				vector<int> neibs=G->neighbors(currNode,color);
				for(unsigned int i=0;i<neibs.size();i++){
					if(Q.contains(neibs[i]) && (G->get_edge_value(neibs[i],currNode)<dist[neibs[i]]) ){
						edges[neibs[i]]=currNode;
						dist[neibs[i]]=G->get_edge_value(neibs[i],currNode);
						Q.chgPrioirity(neibs[i],dist[neibs[i]]);
					}
				}
			}

		}
	}
}
开发者ID:ktjones,项目名称:CPP4C_Programmers,代码行数:36,代码来源:HWFive.cpp

示例3: while

// Return a list<char> containing the list of nodes in the shortest path between 'u' and 'w'
list<char> ShortestPath::path(char u, char w)
{
  // Initialize candidates list with all nodes
  list<char> candidates = graph.vertices(), desiredPath;
  list<NodeInfo> minPaths;
  PriorityQueue p;
  NodeInfo lastSelected, n;
     
  // Calculate shortest path from 'u' to 'w' (Dijkstra's Algorithm)
  candidates.remove(u);			// Remove 'u' from candidates list
  lastSelected.nodeName = u;		// Set 'u' as lastSelected
  lastSelected.minDist = 0;
  lastSelected.through = u;
  minPaths.push_back(lastSelected);	// Add 'u' to minPath list
  while ((!candidates.empty()) && (lastSelected.nodeName !=w))
  {
    // For each node in candidate list calculate the cost to reach that candidate through lastSelected 
    for(list<char>::iterator i=candidates.begin(); i != candidates.end(); ++i)
    {
      n.nodeName=*i;
      n.minDist=lastSelected.minDist+graph.get_edge_value(lastSelected.nodeName,*i);
      n.through=lastSelected.nodeName;
      if (!p.contains(n))	// Add candidate to priority queue if doesn't exist 
	p.insert(n);
      else
	if (p.isBetter(n))	// Update candidate minDist in priority queue if a better path was found
	  p.chgPriority(n);
    }
    lastSelected = p.top();			// Select the candidate with minDist from priority queue
    p.minPriority();				// Remove it from the priority queue
    minPaths.push_back(lastSelected);		// Add the candidate with min distance to minPath list
    candidates.remove(lastSelected.nodeName);	// Remove it from candidates list
  }
  
  // Go backward from 'w' to 'u' adding nodes in that path to desiredPath list
  lastSelected=minPaths.back();
  desiredPath.push_front(lastSelected.nodeName);
  while(lastSelected.nodeName!=u)
  {
    for(list<NodeInfo>::iterator i=minPaths.begin(); i != minPaths.end(); ++i)
      if ((*i).nodeName==lastSelected.through)
      {
	lastSelected=(*i);
	desiredPath.push_front(lastSelected.nodeName);
      }
  }
  return desiredPath;
}
开发者ID:,项目名称:,代码行数:49,代码来源:


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