// Launch unit test.
  EUnitTestResult test()
  { CALL
    CPriorityQueue<Tschar> a;
    UT_ASSERT_ZERO(a.getSize());

    // Fill the first priority queue.
    UT_ASSERT_CHECK_FILL(a);
    UT_ASSERT_EQUAL(a.getSize(), 9);

    // Show the first priority queue.
    UT_ASSERT_CHECK_SHOW(a);

    // Copy priority queues.
    CPriorityQueue<Tschar> b(a);
    UT_ASSERT_EQUAL(b.getSize(), 9);

    // Show the second priority queue.
    UT_ASSERT_CHECK_SHOW(b);

    // Remove items from the first priority queue.
    UT_ASSERT_CHECK_REMOVE(a);
    UT_ASSERT_ZERO(a.getSize());

    // Clear the second priority queue.
    UT_ASSERT(b.clear());
    UT_ASSERT_ZERO(b.getSize());

    // Check serialization functionality.
    UT_ASSERT_CHECK_SERIALIZATION();

    UT_ACCEPT;
  }
Ejemplo n.º 2
0
	/** remove and return the open node specified by a key */
	inline Titem_& PopOpenNode(const Key& key)
	{
		Titem_& item = m_open.Pop(key);
		uint idxPop = m_open_queue.FindIndex(item);
		m_open_queue.Remove(idxPop);
		return item;
	}
Ejemplo n.º 3
0
	/** return the best open node */
	inline Titem_ *GetBestOpenNode()
	{
		if (!m_open_queue.IsEmpty()) {
			return m_open_queue.Begin();
		}
		return NULL;
	}
  // Check the serialization functionality.
  void UT_ASSERT_CHECK_SERIALIZATION()
  { CALL
    CPriorityQueue<Tschar> instance;

    // Fill the priority queue.
    UT_ASSERT_CHECK_FILL(instance);

    // Show the priority queue before serialization.
    UT_ASSERT_CHECK_SHOW(instance);

    // Save instance.
    SaveArchive saver;
    UT_ASSERT(saver.open());
    UT_ASSERT(saver.serialize(instance));
    UT_ASSERT(saver.close());

    // Clear instance.
    UT_ASSERT(instance.clear());

    // Load instance.
    LoadArchive loader;
    UT_ASSERT(loader.open());
    UT_ASSERT(loader.serialize(instance));
    UT_ASSERT(loader.close());

    // Show the priority queue after serialization.
    UT_ASSERT_CHECK_SHOW(instance);
  }
Ejemplo n.º 5
0
	/** remove and return the best open node */
	inline Titem_ *PopBestOpenNode()
	{
		if (!m_open_queue.IsEmpty()) {
			Titem_ *item = m_open_queue.Shift();
			m_open.Pop(*item);
			return item;
		}
		return NULL;
	}
 // Check the fill functionality of the CPriorityQueue<Tschar>.
 void UT_ASSERT_CHECK_FILL(CPriorityQueue<Tschar>& a_rPriorityQueue)
 { CALL
   UT_ASSERT(a_rPriorityQueue.insert(ASC('F'), 6));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('C'), 3));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('G'), 7));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('B'), 2));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('H'), 8));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('A'), 1));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('D'), 4));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('I'), 9));
   UT_ASSERT(a_rPriorityQueue.insert(ASC('E'), 5));
 }
Ejemplo n.º 7
0
	/** insert given item as open node (into m_open and m_open_queue) */
	inline void InsertOpenNode(Titem_& item)
	{
		assert(m_closed.Find(item.GetKey()) == NULL);
		m_open.Push(item);
		m_open_queue.Include(&item);
		if (&item == m_new_node) {
			m_new_node = NULL;
		}
	}
 // Check the remove functionality of the CPriorityQueue<Tschar>.
 void UT_ASSERT_CHECK_REMOVE(CPriorityQueue<Tschar>& a_rPriorityQueue)
 { CALL
   Tuint priority = 1;
   Tschar value = ASC('A');
   CPriorityQueue<Tschar>::TIterator it = a_rPriorityQueue.getItLast();
   while (it.isValid())
   {
     UT_ASSERT((it.getPriorityRef() == priority++) && (*it == value++));
     UT_ASSERT(it.remove());
   }
   UT_ASSERT((priority == 10) && (value == ASC('J')));
 }
 // Check the show functionality of the CPriorityQueue<Tschar>.
 void UT_ASSERT_CHECK_SHOW(const CPriorityQueue<Tschar>& a_crPriorityQueue)
 { CALL
   Tuint priority = 1;
   Tschar value = ASC('A');
   CPriorityQueue<Tschar>::TIteratorConst it = a_crPriorityQueue.getItLast();
   if (it.isValid())
   {
     do
     {
       UT_ASSERT((it.getPriorityRef() == priority++) && (*it == value++));
     } while (it.stepBackward() == 1);
   }
   UT_ASSERT((priority == 10) && (value == ASC('J')));
 }
Ejemplo n.º 10
0
int mainPOJ1125()
{
    int graph[101][101] ;
    while (1) {
        int stockerNumber = 0 ;
        scanf("%d", &stockerNumber) ;
        if (stockerNumber == 0) {
            break ;
        }
        // init the graph to -1
        for (int i = 0; i <= stockerNumber; ++i) {
            for (int j = 0; j < stockerNumber; ++j) {
                if (i == j) {
                    graph[i][j] = 0 ;
                } else {
                    graph[i][j] = INT_MAX ;
                }
            }
        }
        // construct the graph
        for (int stockerIndex = 1; stockerIndex <= stockerNumber; ++stockerIndex) {
            int contacts = 0 ;
            scanf("%d", &contacts) ;
            for (int i = 0; i < contacts; ++i) {
                int contactsIndex = 0 ;
                int time = 0 ;
                scanf("%d%d", &contactsIndex, &time) ;
                graph[stockerIndex][contactsIndex] = time ;
            }
        }
        int *distance = (int *)malloc(sizeof(int)*(stockerNumber+1)) ;
        int minPath = INT_MAX ;
        int sourceIndex = INT_MIN ;
        for (int stockerIndex = 1; stockerIndex <= stockerNumber; ++stockerIndex) {
            int source = stockerIndex ;
            // init the distance
            for (int i = 1 ; i <= stockerNumber; ++i) {
                distance[i] = INT_MAX ;
            }
            distance[source] = 0 ;
            
            vector<PriorityQueueObject *> vecterPQO ;
            CPriorityQueue priorityQue = CPriorityQueue(stockerNumber) ;
            for (int i = 1; i <= stockerNumber; ++i) {
                PriorityQueueObject *obj = new PriorityQueueObject ;
                // element is index of stocker
                obj->element = i ;
                obj->priority = distance[i] ;
                priorityQue.enqueue(obj) ;
                vecterPQO.push_back(obj) ;
            }
            
            while (priorityQue.getSize() > 0) {
                PriorityQueueObject *obj = priorityQue.extractMin() ;
                int indexMin = obj->element ;
                int distanceMin = obj->priority ;
                if (distanceMin == INT_MAX) {
                    // disjoint and free left objects
                    break ;
                }
                // enumerate all the edges that indexMin connects to
                for (int i = 1; i <= stockerNumber; ++i) {
                    if (graph[indexMin][i] > 0 && graph[indexMin][i] != INT_MAX) {
                        // get rid of self and the one has no edge with indexMin
                        if (distance[i] > distanceMin + graph[indexMin][i]) {
                            // relex the edge
                            distance[i] = distanceMin + graph[indexMin][i] ;
                            PriorityQueueObject *objRelax = vecterPQO[i-1] ;
                            objRelax->priority = distance[i] ;
                            priorityQue.decreaseObj(objRelax) ;
                        }
                    }
                }
            }
            
            int maxPathForSepSource = distance[1] ;
            for (int i = 1; i <= stockerNumber; ++i) {
                maxPathForSepSource = distance[i] > maxPathForSepSource ? distance[i] : maxPathForSepSource ;
            }
            if (maxPathForSepSource < minPath) {
                minPath = maxPathForSepSource ;
                sourceIndex = source ;
            }
            
            // free
            vector<PriorityQueueObject *>::iterator iter ;
            for (iter = vecterPQO.begin(); iter != vecterPQO.end(); iter++) {
                PriorityQueueObject *obj = *iter ;
                delete obj ;
            }
            vecterPQO.clear() ;
        }
        if (minPath == INT_MAX) {
            printf("disjoint\n") ;
        } else {
            printf("%d %d\n", sourceIndex, minPath) ;
        }
        free(distance) ;
    }
	return 0;
}