本文整理汇总了C++中PriorityQueue::top方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::top方法的具体用法?C++ PriorityQueue::top怎么用?C++ PriorityQueue::top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::top方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(){
ios::sync_with_stdio(false);
ll n, idx = 0, val, rs; cin >> n;
Node nd; cin >> nd.x >> nd.y;
val = nd.x, nd.diff = nd.y - nd.x + 1, Score.push_back(nd);
for (int i = 1; i < n; i++){
cin >> nd.x >> nd.y;
nd.diff = nd.y - nd.x + 1;
if (nd.x > val) idx++;
Score.push_back(nd);
}
rs = idx;
sort(all(Score), cmp);
PriorityQueue pq;
int lstidx = idx + 1;
for (int i = 0; i < idx; i++){
pq.add(Score[i].diff);
}
while (val >= pq.top()){
if (!rs || pq.top() == -1) break;
if (pq.top() <= val){
val -= pq.top();
pq.remove();
idx--;
}
while (lstidx < n && Score[lstidx].x > val){
pq.add(Score[lstidx].diff);
lstidx++;
idx++;
}
rs = min(rs, idx);
}
cout << rs + 1 << ln;
return 0;
}
示例2: e
// Return a list<Edge> containing the list of edges in the minimum spanning tree found by Kruskal's Algorithm
list<Edge> KruskalMST::tree() {
list<Edge> mst;
PriorityQueue<Edge> p;
Node selected;
Edge selectedEdge;
// Add each node of the graph to its own set
list<Node> allNodes = graph.vertices();
for(list<Node>::iterator i=allNodes.begin(); i != allNodes.end(); ++i)
makeSet(*i);
// Insert all edges from the graph into priority queue
for(list<Node>::iterator i=allNodes.begin(); i != allNodes.end(); ++i) {
list<Node> iEdges = graph.neighbors((*i).getNodeNumber());
for(list<Node>::iterator j=iEdges.begin(); j != iEdges.end(); ++j) {
Edge e((*i).getNodeNumber(),(*j).getNodeNumber(),(*j).getEdgeWeight());
p.insert(e);
}
}
while(p.size() != 0) {
selectedEdge = p.top(); // Remove the lower edge from the priority queue
if (findSet(selectedEdge.getSrc()) != findSet(selectedEdge.getDst())) {
mst.push_back(selectedEdge); // If src and dst are in different sets, then add this edge to mst
combineSet(selectedEdge.getSrc(),selectedEdge.getDst()); // Combine both sets into one
}
}
return mst;
}
示例3: skipToTs
double skipToTs(double ts)
{
double ret = -1000000000.0;
for(int i = 0; i < 100; i++){
// "tick" the video, filling up the frame queue
tick(true);
// throw away any frames below the requested time
double curr = .0;
while(frameQueue.GetContainer().size() > 0 && (curr = timeFromTs(frameQueue.GetContainer().at(0)->GetPts())) < ts){
ret = curr;
frameQueue.GetContainer().erase(frameQueue.GetContainer().begin());
}
// done if the frameQueue has a timestamp equal to or larger than the requested time
if(frameQueue.size() > 0 && timeFromTs(frameQueue.top()->GetPts()) >= ts)
break;
}
if(frameQueue.size() > 0){
// return the actual timestamp achieved
ret = timeFromTs(frameQueue.GetContainer().at(0)->GetPts());
audioHandler->discardQueueUntilTs(ret);
}
return ret;
}
示例4: uf
// @snip <sh19910711/contest:setlib/disjoints_set.cpp>
template <typename GraphType> const typename std::vector<typename GraphType::Edge> get_minimum_spanning_forest( const GraphType& G ) {
typedef typename std::vector<typename GraphType::Edge> Edges;
typedef typename GraphType::Edge GraphEdge;
typedef typename GraphType::Edges GraphEdges;
typedef std::priority_queue<GraphEdge, Edges, std::greater<GraphEdge> > PriorityQueue;
typedef setlib::DisjointSets UnionFind;
Edges res;
PriorityQueue E;
UnionFind uf(G.num_vertices);
for ( int i = 0; i < G.num_vertices; ++ i ) {
for ( typename GraphEdges::const_iterator it_i = G.vertex_edges[i].begin(); it_i != G.vertex_edges[i].end(); ++ it_i ) {
const GraphEdge& e = **it_i;
E.push(GraphEdge(e.from, e.to, e.weight));
}
}
while ( ! E.empty() ) {
GraphEdge e = E.top();
E.pop();
if ( ! uf.same(e.from, e.to) ) {
res.push_back(e);
uf.merge(e.from, e.to);
}
}
return res;
}
示例5: dumpContents
void dumpContents( const string & msg, PriorityQueue & pq )
{
cout << msg << ":" << endl;
while( !pq.empty( ) )
{
cout << pq.top( ) << endl;
pq.pop( );
}
}
示例6: main
int main(){
PriorityQueue<int> pq;
for(int i=0;i<10;i++){
pq.push(i);
}
while(!pq.empty()){
cout<<pq.top()<<" ";
pq.pop();
}
}
示例7: fetchFrame
FramePtr fetchFrame()
{
bool wasStepIntoQueue = stepIntoQueue;
if(frameQueue.empty() && IsEof() && !reportedEof)
{
reportedEof = true;
messageCallback(MEof, "eof");
}
if(stepIntoQueue && !frameQueue.empty())
{
stepIntoQueue = false;
timeHandler->SetTime(timeFromTs(frameQueue.top()->GetPts()) + .001);
audioHandler->discardQueueUntilTs(timeHandler->GetTime());
}
double time = timeHandler->GetTime();
FramePtr newFrame = 0;
// Throw away all old frames (timestamp older than now) except for the last
// and set the pFrame pointer to that.
int poppedFrames = 0;
while(!frameQueue.empty() && timeFromTs(frameQueue.top()->GetPts()) < time)
{
newFrame = frameQueue.top();
frameQueue.pop();
poppedFrames++;
}
if(poppedFrames > 1){
FlogD("skipped " << poppedFrames - 1 << " frames");
}
if(newFrame != 0 && (newFrame->GetPts() >= time || wasStepIntoQueue))
return newFrame;
return 0;
}
示例8: inpainting_loop
inline
void inpainting_loop(VertexListGraph& g, InpaintingVisitorType vis,
BoundaryStatusMap& boundaryStatusMap, PriorityQueue& boundaryNodeQueue,
NearestNeighborFinder find_inpainting_source,
PatchInpainter inpaint_patch)
{
typedef typename boost::graph_traits<VertexListGraph>::vertex_descriptor Vertex;
typedef typename boost::graph_traits<VertexListGraph>::edge_descriptor Edge;
typedef typename boost::property_traits<PositionMap>::value_type PositionValueType;
// When this function is called, the priority-queue should already be filled
// with all the hole-vertices (which should also have "Color::black()" color value).
// So, the only thing this function does is run the inpainting loop. All of the
// actual code is externalized in the functors and visitors (vis, find_inpainting_source, inpaint_patches, etc.).
while(true)
{
// find the next target to in-paint:
Vertex targetNode;
do
{
if( boundaryNodeQueue.empty() )
{
std::cout << "Queue is empty, exiting." << std::endl;
return; //terminate if the queue is empty.
}
targetNode = boundaryNodeQueue.top();
boundaryNodeQueue.pop();
} while( get(boundaryStatusMap, targetNode) == false );
// Notify the visitor that we have a hole target center.
vis.discover_vertex(targetNode, g);
// next, we want to find a source patch-center that matches best to our target-patch:
Vertex source_patch_center = find_inpainting_source(targetNode);
vis.vertex_match_made(targetNode, source_patch_center, g);
// finally, do the in-painting of the target patch from the source patch.
// the inpaint_patch functor should take care of iterating through the vertices in both
// patches and call "vis.paint_vertex(target, source, g)" on the individual vertices.
inpaint_patch(targetNode, source_patch_center, g, vis);
if(!vis.accept_painted_vertex(targetNode, g))
{
throw std::runtime_error("Vertex was not painted successfully!");
}
vis.finish_vertex(targetNode, g);
} // end main iteration loop
};
示例9: getNode
Node getNode(char name){
if (find(name)){
PriorityQueue tempQueue;
while (top().name != name){
tempQueue.push(top());
pop();
}
Node node = top();
while (!tempQueue.empty()){
push(tempQueue.top());
tempQueue.pop();
}
return node;
}
}
示例10: 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;
}
示例11: A_star
static bool A_star(DGR& dg, Weight& w, Digraph::Node s, Digraph::Node t, int k, DistMap& dist, PredMap& pred)
{
if(s == t) return false;
typedef QNode Node;
typedef std::vector<QNode> NodeCon;
typedef std::priority_queue<Node,NodeCon> PriorityQueue;
PriorityQueue pq;
int cnt = 0;
pred[s] = INVALID;
pq.push(Node(s, INVALID, 0, dist[s]));
while(!pq.empty())
{
Node node = pq.top();
Digraph::Node u = node.u;
pred[u] = node.parent;
//cout<<"->pop:f("<<Digraph::id(u)<<")="<<node.g+p.h
// <<"\tg("<<Digraph::id(u)<<")="<<node.g
// <<"\th("<<Digraph::id(u)<<")="<<node.h
// <<endl;
pq.pop();
if(u == t)
{
//cout<<"---------------------"<<endl;
cnt++;
}
if(cnt == k)
{
break;
}
for(typename DGR::OutArcIt e(dg,u);e!=INVALID;++e)
{
Digraph::Node v = dg.target(e);
pred[v] = u;
Node child_node(v, u, node.g + w[e], dist[v]);
//cout<<"\t->push:f("<<Digraph::id(v)<<")="<<child_node.g+child_node.h
//<<"\tg("<<Digraph::id(v)<<")="<<child_node.g
//<<"\th("<<Digraph::id(v)<<")="<<child_node.h
//<<endl;
pq.push(child_node);
}
}
return (cnt == k);
}
示例12: adjustTime
void adjustTime(){
// Checks so that the current time doesn't diff too much from the decoded frames.
// If it does the stream probably jumped ahead or back, so current time needs to
// be adjusted accordingly.
if(frameQueue.empty())
return;
double time = timeHandler->GetTime();
double pts = timeFromTs(frameQueue.top()->GetPts());
// If the next frame is far into the future or the past,
// set the time to now
if(pts > time + 100.0 || pts < time - 100.0){
FlogD("adjusted time");
timeHandler->SetTime(pts);
}
}
示例13: sweep
inline void sweep(Range const& range, PriorityQueue& queue,
InitializationVisitor& initialization_visitor,
EventVisitor& event_visitor,
InterruptPolicy const& interrupt_policy)
{
typedef typename PriorityQueue::value_type event_type;
initialization_visitor.apply(range, queue, event_visitor);
while (! queue.empty())
{
event_type event = queue.top();
queue.pop();
event_visitor.apply(event, queue);
if (interrupt_policy.enabled && interrupt_policy.apply(event))
{
break;
}
}
geofeatures_boost::ignore_unused(interrupt_policy);
}
示例14:
void myerase(Node val)
{
PriorityQueue <Node, std::vector<Node>, Compare> tmp;
while (this->c.size() > 0)
{
auto first = this->c.begin();
if (first->getTab() == val.getTab())
{
this->pop();
break;
}
tmp.push(*first);
this->pop();
}
while (tmp.size() > 0)
{
this->push(tmp.top());
tmp.pop();
}
}
示例15: DijkstraDistances
// DESC: Dijkstra's algorithm to find shortest distance from source to destination on a graph.
// INPUT: Graph to expand on, Vertex to represent as the source.
// OUTPUT: Linked list of the path
LinkedListT* DijkstraDistances(Graph* graph, Vertex* source) {
// form a cloud to every point.
// Store this cloud in the PQueue.
LinkedListT* verticies = graph->verticies();
PriorityQueue* pq = new PriorityQueue();
// loop through verticies
for(int i=0; i<verticies->size(); i++) {
Vertex* vv = (Vertex*)verticies->get(i);
if(vv == source) {
vv->distance = 0; // set vertex to zero if it is the source vertex.
} else {
vv->distance = 999999999; // set verticies to infinity to represent an unreachable location.
}
// insert into queue.
pq->insert(vv->distance,vv);
}
// empty out the set
while(!pq->empty()) {
// remove the minimum distance from the set. (usually source vertex on start)
Vertex* v = (Vertex*)pq->removeMin();
LinkedListT* vt = v->incidentEdges;
// loop through incident Edges
for(int i=0; i<vt->size(); i++) {
Edge* e = (Edge*)vt->get(i);
Vertex* v2 = e->opposite(v);
int r = v->distance + e->data;
// if the total range is less than v2's distance, then set it.
if(r < v2->distance) {
v2->distance = r;
pq->replaceKey(pq->top(),(void*)v2,r);
}
}
}
}