コード例 #1
0
/*function to update the dist, pred arrays, as well as ensure the priority
  queue is up to scratch */
void update(int v,int current,int pred[], int dist[], priority_queue *pq,
		int **costs) {
	/*update dist/pred arrays */
	dist[v]=dist[current]+costs[current][v];
	pred[v]=current;
	/*update the priority queue in the right position, using its built in
	  hash table to ensure the correct index gets updated. This newly
	  updated node is then bubbled up to ensure the heap property is 
	  maintained */
	decrease_priority(pq,pq->hash[v],dist[v]);
	if (DEBUG) printf("The new distance to %d is: %d through %d \n",
							v,dist[v],pred[v]); 
}
int main()
{
  record queue[10000]; 
	long int ele, pr;
	char ch;
	scanf("%c",&ch);
  getchar();
  do
  {
    switch(ch)
    {
	    case 'a':
		    scanf("%ld",&ele);
		    scanf("%ld",&pr);
		    getchar();
        if(checkNumber(ele) && checkNumber(pr) && ele >= 0 && pr >=1)
        {
		      insert(queue, ele, pr);
          size++;
        }
        else
        {
          printf("Enter elements in given range.\n");
        }
		    break;
    
        case 'e':
      	extract_min(queue);
        break;
   
      case 'g':
				get_min(queue);
        break;
      
      case 'd':
		    scanf("%ld",&ele);
		    scanf("%ld",&pr);
		    getchar();
        if(checkNumber(ele) && checkNumber(pr) && ele >= 0 && pr >=1)
        {
          decrease_priority(queue, ele, pr);
        }
        else
        {
          printf("Enter elements in given range.\n");
        }
      	
        break;
  
      case 's':
        printf("\n");
        break;
        
      default:
        printf("Enter correct choice.\n");
        break;
    }
	scanf("%c",&ch);
  getchar();
  } while(ch != 's');

  return 0;
}