예제 #1
0
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);
}
예제 #2
0
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]));
            }
        }
    }
}
예제 #3
0
파일: TestHeap.cpp 프로젝트: jinrui/ministl
		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;
		}
예제 #4
0
 int findKthLargest(vector<int>& nums, int k) {
     int n = nums.size();
     Operator opt(k < n/2);
     if (k >= n/2) {
         k = n+1-k;
     }
     buildHeap(nums, opt);
     for (int i=0; i<k-1; i++) popHeap(nums, n--, opt);
     return nums[0];
 }
예제 #5
0
void heapSort(int * array, size_t length) {
	int * array_copy = malloc(sizeof(int) * length);
	for (size_t i = 0; i < length; ++i) {
		array_copy[i] = array[i];
	}
	Heap heap = {array_copy, length};
	buildMinHeap(&heap);
	for (size_t i = 0; i < length; ++i)  {;
		array[i] = popHeap(&heap);
	}
	free(array_copy);
}
예제 #6
0
// deletes this_node from the heap, and then repairs the heap
void deleteNodeFromHeap(node* this_node)
{ 
  this_node->inHeap = closed;
  tempInd = this_node->heapIndex;  
  this_node->heapIndex = -1;
  
  // if this was the last node on the heap, then we are done
  if(tempInd == indexOfLast)
  {
    indexOfLast--;
    
    if(indexOfLast == 0)
      parentOfLast = -1;
    else
      parentOfLast = (indexOfLast-1)/2;
    
    return;
  }
  else if(tempInd == 0) // if this is the top node, then just pop it
  {
    popHeap();
    return;
  }  
  
  // put last node from heap where this node used to be
  heapNode[tempInd] = heapNode[indexOfLast];
  heapNode[tempInd]->heapIndex = tempInd;
  
  // take care of heap values at old last node position
  heapNode[indexOfLast] = NULL;
  indexOfLast--;
  
  if(indexOfLast == 0)
    parentOfLast = -1;
  else
    parentOfLast = (indexOfLast-1)/2;
  
  // need to repair heap differently depending on the key vector of heapNode[tempInd]
  if(tempInd > parentOfLast) // then can't go down any more so only try to go up
  {
    bubbleUp(tempInd);
  }
  else if(nodeLess(heapNode[tempInd], heapNode[(tempInd-1)/2])) // this node is less than its parent so try to go up
  {
      bubbleUp(tempInd);
  }
  else // this node is not less than its parent, so try to go down
  {
    bubbleDown(tempInd); 
  }
}
예제 #7
0
파일: NNI.c 프로젝트: Ward9250/ape
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]);
    }
}
예제 #8
0
파일: NNI.c 프로젝트: Ward9250/ape
//void NNI(tree *T, double **avgDistArray, int *count)
void NNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies)
{
  edge *e, *centerEdge;
  edge **edgeArray;
  int *location;
  int *p,*q;
  int i,j;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (edge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));

  double epsilon = 0.0;
  for (i=0; i<numSpecies; i++)
    for (j=0; j<numSpecies; j++)
      epsilon += D[i][j];
  epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON;

  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
  e = findBottomLeft(T->root->leftEdge);
  /* *count = 0; */
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	NNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  while (weights[p[1]] + epsilon < 0)
    {
      centerEdge = edgeArray[p[1]];
      (*count)++;
      T->weight = T->weight + weights[p[1]];
      NNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the NNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other four edges*/
      e = centerEdge->head->leftEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = centerEdge->head->rightEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = siblingEdge(centerEdge);
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = centerEdge->tail->parentEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
}
예제 #9
0
void bNNI(meTree *T, double **avgDistArray, int *count)
{
  meEdge *e, *centerEdge;
  meEdge **edgeArray;
  int *p, *location, *q;
  int i;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (meEdge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));
  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
  if (verbose)
    {
      assignBalWeights(T,avgDistArray);
      weighTree(T);
    }
  e = findBottomLeft(T->root->leftEdge);
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  /*NOTE: the loop below should test that weights[p[1]] < 0, but
    if compiled with optimization it is possible that weights[p[1]]
    ends up negative and very small, so that changes to the heap
    become cyclic and the loop becomes infinite. To avoid this
    behavior, stop the loop short of 0.0. This is a workaround 
    until the roundoff sensitivity is removed algorithmically */
  while (weights[p[1]] < -1e-8)
    {
      centerEdge = edgeArray[p[1]];
      (*count)++;
      if (verbose)
	{
	  T->weight = T->weight + weights[p[1]];
	  printf("New tree weight is %lf.\n",T->weight);
	}
      bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the bNNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other edges of T*/
      /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/
      e = depthFirstTraverse(T,NULL);
      while (NULL != e)
	{
	  bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
	  e = depthFirstTraverse(T,e);
	}
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
  assignBalWeights(T,avgDistArray);
}
예제 #10
0
파일: bNNI.c 프로젝트: cran/ape
//void bNNI(tree *T, double **avgDistArray, int *count)
void bNNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies)
{
    edge *e;//, *centerEdge deleted by EP, 2013-09-26, see also below
  edge **edgeArray;
  int *p, *location, *q;
  int i,j;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (edge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));

  double epsilon = 0.0;
  for (i=0; i<numSpecies; i++)
    for (j=0; j<numSpecies; j++)
      epsilon += D[i][j];
  epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON;

  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
/*  if (verbose)
    {
      assignBMEWeights(T,avgDistArray);
      weighTree(T);
    }*/
  e = findBottomLeft(T->root->leftEdge);
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  while (weights[p[1]] + epsilon < 0)
    {
	/* centerEdge = edgeArray[p[1]]; apparently unused later, deleted by EP, 2013-09-26 */
      (*count)++;
/*      if (verbose)
	{
	  T->weight = T->weight + weights[p[1]];
	  printf("New tree weight is %lf.\n",T->weight);
	}*/
      bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the bNNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other edges of T*/
      /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/
      e = depthFirstTraverse(T,NULL);
      while (NULL != e)
	{
	  bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
	  e = depthFirstTraverse(T,e);
	}
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
  free(weights);
  assignBMEWeights(T,avgDistArray);
}
예제 #11
0
파일: bNNI.c 프로젝트: pranjalv123/ASTRID
void bNNI (tree *T, double **avgDistArray, int *count, FILE *statfile)
{
	edge *e;
	edge **edgeArray;
	int *p, *location, *q;
	int i;
	int possibleSwaps;
	double *weights;

	p = initPerm (T->size+1);
	q = initPerm (T->size+1);
	edgeArray = (edge **) mCalloc ((T->size+1), sizeof (edge *));
	weights = (double *) mCalloc ((T->size+1), sizeof (double));
	location = (int *) mCalloc ((T->size+1), sizeof (int));
	for (i=0; i<T->size+1; i++)
	{
		weights[i] = 0.0;
		location[i] = NONE;
	}

	assignBMEWeights (T, avgDistArray);
	weighTree (T);
	if (!isBoostrap)
	{
		if (statfile)
			fprintf (statfile, "\tBefore NNI:     tree length is %f.\n", T->weight);
		if (verbose > 2)
			Debug ( (char*)"Before NNI: tree length is %f.", T->weight);
		else if (verbose > 1)
			Message ( (char*)". Before NNI: tree length is %f.", T->weight);
	}

	e = findBottomLeft (T->root->leftEdge);
	while (NULL != e)
	{
		edgeArray[e->head->index+1] = e;
		location[e->head->index+1] = bNNIEdgeTest (e, T, avgDistArray,
			weights + e->head->index + 1);
		e = depthFirstTraverse (T,e);
	}

	possibleSwaps = makeThreshHeap (p, q, weights, T->size+1,0.0);
	permInverse (p, q, T->size+1);

	/* We put the negative values of weights into a heap, indexed by p
	 * with the minimum value pointed to by p[1]
	 * p[i] is index (in edgeArray) of edge with i-th position in the
	 * heap, q[j] is the position of edge j in the heap */

	while (weights[p[1]] < -DBL_EPSILON)
	{
		(*count)++;
		T->weight = T->weight + weights[p[1]];
		if (!isBoostrap)
		{
			if (statfile)
				fprintf (statfile, "\tNNI  %5d: new tree length is %f.\n", *count, T->weight);
			if (verbose > 2)
				Debug ( (char*)"NNI %5d: new tree length is %f.", *count, T->weight);
			else if (verbose > 1)
				Message ( (char*)". NNI %5d: new tree length is %f.", *count, T->weight);
		}

		bNNItopSwitch (edgeArray[p[1]], location[p[1]], avgDistArray);
		location[p[1]] = NONE;
		weights[p[1]] = 0.0;	//after the bNNI, this edge is in optimal configuration
		popHeap (p, q, weights, possibleSwaps--, 1);

		/* but we must retest the other edges of T */
		/* CHANGE 2/28/2003 expanding retesting to _all_ edges of T */

		e = depthFirstTraverse (T, NULL);
		while (NULL != e)
		{
			bNNIRetestEdge (p, q, e, T, avgDistArray, weights, location, &possibleSwaps);
			e = depthFirstTraverse (T, e);
		}
	}

	free (p);
	free (q);
	free (location);
	free (edgeArray);
	free (weights);
	assignBMEWeights (T, avgDistArray);

	return;
}