home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / graph.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  6KB  |  169 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * graph.cpp - Graph program.  Section 9.3.2
  6.  *
  7.  * $Id: graph.cpp,v 1.15 1996/09/28 00:28:01 philippe Exp $
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  12.  * ALL RIGHTS RESERVED *
  13.  * The software and information contained herein are proprietary to, and
  14.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  15.  * intends to preserve as trade secrets such software and information.
  16.  * This software is furnished pursuant to a written license agreement and
  17.  * may be used, copied, transmitted, and stored only in accordance with
  18.  * the terms of such license and with the inclusion of the above copyright
  19.  * notice.  This software and information or any other copies thereof may
  20.  * not be provided or otherwise made available to any other person.
  21.  *
  22.  * Notwithstanding any other lease or license that may pertain to, or
  23.  * accompany the delivery of, this computer software and information, the
  24.  * rights of the Government regarding its use, reproduction and disclosure
  25.  * are as set forth in Section 52.227-19 of the FARS Computer
  26.  * Software-Restricted Rights clause.
  27.  * 
  28.  * Use, duplication, or disclosure by the Government is subject to
  29.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  30.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  31.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  32.  * P.O. Box 2328, Corvallis, Oregon 97339.
  33.  *
  34.  * This computer software and information is distributed with "restricted
  35.  * rights."  Use, duplication or disclosure is subject to restrictions as
  36.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  37.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  38.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  39.  * then the "Alternate III" clause applies.
  40.  *
  41.  **************************************************************************/
  42.  
  43. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  44. #include <map>
  45. #include <vector>
  46. #include <queue>
  47. #else
  48. #include <map.hpp>
  49. #include <vector.hpp>
  50. #include <queue.hpp>
  51. #endif
  52.  
  53. #ifdef _RW_STD_IOSTREAM
  54. #include <iostream>
  55. #else
  56. #include <iostream.h>
  57. #endif
  58.     
  59. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  60. #include <string>
  61. #else
  62. #include <string.hpp>
  63. #endif
  64. #ifndef _RWSTD_NO_NAMESPACE
  65.   using namespace std;
  66.   using namespace std::rel_ops;
  67. #endif
  68.  
  69. typedef map<string, int, less<string>,allocator<string>  >          stringVector;
  70. typedef map<string, stringVector, less<string>,allocator<string>  > graph;
  71.  
  72. struct DistancePair
  73. {
  74.     unsigned first;
  75.     string   second;
  76.     DistancePair() : first(0) {}
  77.     DistancePair(unsigned f, const string& s) : first(f), second(s) {}
  78. };
  79.  
  80. bool operator< (const DistancePair& lhs, const DistancePair& rhs)
  81. {
  82.     return lhs.first < rhs.first;
  83. }
  84.  
  85. bool operator> (const DistancePair& lhs, const DistancePair& rhs)
  86. {
  87.     return lhs.first > rhs.first;
  88. }
  89.  
  90. string pendleton("Pendleton");
  91. string pensacola("Pensacola");
  92. string peoria("Peoria");
  93. string phoenix("Phoenix");
  94. string pierre("Pierre");
  95. string pittsburgh("Pittsburgh");
  96. string princeton("Princeton");
  97. string pueblo("Pueblo");
  98.  
  99. graph cityMap;
  100.  
  101. void shortestDistance (graph& cityMap, string& start, stringVector& distances)
  102. {
  103.     //
  104.     // Process a priority queue of distances to nodes.
  105.     //
  106.     priority_queue<DistancePair, vector<DistancePair,allocator<DistancePair> >,
  107.         greater<DistancePair> > que;
  108.     que.push(DistancePair(0, start));
  109.     
  110.     while (! que.empty())
  111.     {
  112.         //
  113.         // Pull nearest city from queue.
  114.         //
  115.         int distance = que.top().first;
  116.         string city = que.top().second;
  117.         que.pop();
  118.         //
  119.         // If we haven't seen it already, process it.
  120.         //
  121.         if (0 == distances.count(city))
  122.         {
  123.             //
  124.             // Then add it to shortest distance map.
  125.             //
  126.             distances[city] = distance;
  127.             //
  128.             // And put values into queue.
  129.             //
  130.             const stringVector& cities = cityMap[city];
  131.             stringVector::const_iterator start = cities.begin();
  132.             stringVector::const_iterator stop  = cities.end();
  133.             for (; start != stop; ++start) 
  134.                 que.push(DistancePair(distance + (*start).second,
  135.                                       (*start).first));
  136.         }
  137.     }
  138. }
  139.  
  140. int main ()
  141. {
  142.     cout << "Graph example program, from Chapter 7" << endl;
  143.  
  144.     cityMap[pendleton][phoenix]    = 4;
  145.     cityMap[pendleton][pueblo]     = 8;
  146.     cityMap[pensacola][phoenix]    = 5;
  147.     cityMap[peoria][pittsburgh]    = 5;
  148.     cityMap[peoria][pueblo]        = 3;
  149.     cityMap[phoenix][peoria]       = 4;
  150.     cityMap[phoenix][pittsburgh]   = 10;
  151.     cityMap[phoenix][pueblo]       = 3;
  152.     cityMap[pierre][pendleton]     = 2;
  153.     cityMap[pittsburgh][pensacola] = 4;
  154.     cityMap[princeton][pittsburgh] = 2;
  155.     cityMap[pueblo][pierre]        = 3;
  156.     
  157.     stringVector distances;
  158.     
  159.     shortestDistance(cityMap, pierre, distances);
  160.     stringVector::iterator where;
  161.     for (where = distances.begin(); where != distances.end(); ++where)
  162.         cout << "Distance to: " << (*where).first << ":"
  163.              <<  (*where).second << endl;
  164.         
  165.     cout << "End of graph example program" << endl;
  166.  
  167.     return 0;
  168. }
  169.