void search(Step start){ int x, y; for (x = 0; x < mapSize.width; x++){ for (y = 0; y < mapSize.height; y++){ visited[x][y] = 0; } } pushHeap(start); while(heapIndex >= 0){ Step step = popHeap(); if(finished(step)){ //printf("FOUND: %d, %d.\tValue: %d\tIndex: %d\n", step.point.x, step.point.y, step.distance, heapIndex); min = step.distance; break; } if(visited[step.point.x][step.point.y]){ continue; } visited[step.point.x][step.point.y] = 1; int i; for(i = 0; i < 8; i++){ Point next = nextPoint(step.point, i); if(isValidPoint(next)){ pushHeap(makeStep(next, step.distance + map[next.x][next.y])); } } } }
void slove() { int n, e,e1,e2; long long cost; //priority_queue< int, vector<int>, greater<int> >que; Heap *H; scanf("%d", &n); H= createHeap(n); while(n--) { scanf("%d",&e); //que.push(e); pushHeap(H,e); } cost = 0; //while(que.size() > 1) while(H->size > 1) { // e1 = que.top(),que.pop(); // e2 = que.top(),que.pop(); e1 = popHeap(H); e2 = popHeap(H); cost += e1 + e2; //que.push(e1+e2); pushHeap(H,e1+e2); } printf("%lld\n", cost); destoryHeap(H); }
void TestHeap::testCase1() { int ia[9] = {0,1,2,3,4,8,9,3,5}; Vector<int> ivec(ia, ia+9); makeHeap(ivec.begin(), ivec.end()); for(int i=0; i<ivec.size(); ++i) std::cout << ivec[i] << ' '; // 9 5 8 3 4 0 2 3 1 std::cout << std::endl; ivec.push_back(7); pushHeap(ivec.begin(), ivec.end()); for(int i=0; i<ivec.size(); ++i) std::cout << ivec[i] << ' '; // 9 7 8 3 5 0 2 3 1 4 std::cout << std::endl; popHeap(ivec.begin(), ivec.end()); std::cout << ivec.back() << std::endl; // 9. return but no remove. ivec.pop_back(); // remove last elem and no return for(int i=0; i<ivec.size(); ++i) std::cout << ivec[i] << ' '; std::cout << std::endl; // 8 7 4 3 5 0 2 3 1 sortHeap(ivec.begin(), ivec.end()); for(int i=0; i<ivec.size(); ++i) std::cout << ivec[i] << ' '; // 0 1 2 3 3 4 5 7 8 std::cout << std::endl; std::cout << "case1 passed" << std::endl; }
void NNIRetestEdge(int *p, int *q, edge *e,tree *T, double **avgDistArray, double *weights, int *location, int *possibleSwaps) { int tloc; tloc = location[e->head->index+1]; location[e->head->index+1] = NNIEdgeTest(e,T,avgDistArray,weights + e->head->index+1); if (NONE == location[e->head->index+1]) { if (NONE != tloc) popHeap(p,q,weights,(*possibleSwaps)--,q[e->head->index+1]); } else { if (NONE == tloc) pushHeap(p,q,weights,(*possibleSwaps)++,q[e->head->index+1]); else reHeapElement(p,q,weights,*possibleSwaps,q[e->head->index+1]); } }