void dijkstra( unsigned const s, vector<Node<T> > & vs, T* d) { const unsigned n=vs.size(); COLA_ASSERT(s<n); for(unsigned i=0;i<n;i++) { vs[i].id=i; vs[i].d=numeric_limits<T>::max(); vs[i].p=NULL; } vs[s].d=0; PairingHeap<Node<T>*,CompareNodes<T> > Q; for(unsigned i=0;i<n;i++) { vs[i].qnode = Q.insert(&vs[i]); } while(!Q.isEmpty()) { Node<T> *u=Q.extractMin(); d[u->id]=u->d; for(unsigned i=0;i<u->neighbours.size();i++) { Node<T> *v=u->neighbours[i]; T w=u->nweights[i]; if(u->d!=numeric_limits<T>::max() && v->d > u->d+w) { v->p=u; v->d=u->d+w; Q.decreaseKey(v->qnode,v); } } } }
// Test program int main( ) { PairingHeap<int> h; int numItems = 4000; int i = 37; int j; cout << "Checking; no bad output is good" << endl; for( i = 37; i != 0; i = ( i + 37 ) % numItems ) h.insert( i ); for( i = 1; i < numItems; i++ ) { int x; h.deleteMin( x ); if( x != i ) cout << "Oops! " << i << endl; } vector<PairingHeap<int>::Position> p( numItems ); for( i = 0, j = numItems / 2; i < numItems; i++, j =(j+71)%numItems ) p[ j ] = h.insert(j + numItems ); for( i = 0, j = numItems / 2; i < numItems; i++, j =(j+53)%numItems ) h.decreaseKey( p[ j ], j ); i = -1; PairingHeap<int> h2; h2 = h; while( !h2.isEmpty( ) ) { int x; h2.deleteMin( x ); if( x != ++i ) cout << "Oops! " << i << endl; } cout << "Check completed" << endl; return 0; }