miles_span.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. // Sample output:
  10. //
  11. // The graph miles(100,0,0,0,0,10,0) has 405 edges,
  12. // and its minimum spanning tree has length 14467.
  13. //
  14. #include <boost/config.hpp>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <boost/graph/stanford_graph.hpp>
  18. #include <boost/graph/prim_minimum_spanning_tree.hpp>
  19. // A visitor class for accumulating the total length of the minimum
  20. // spanning tree. The Distance template parameter is for a
  21. // PropertyMap.
  22. template <class Distance>
  23. struct total_length_visitor : public boost::dijkstra_visitor<> {
  24. typedef typename boost::property_traits<Distance>::value_type D;
  25. total_length_visitor(D& len, Distance d)
  26. : _total_length(len), _distance(d) { }
  27. template <class Vertex, class Graph>
  28. inline void finish_vertex(Vertex s, Graph& g) {
  29. _total_length += boost::get(_distance, s);
  30. }
  31. D& _total_length;
  32. Distance _distance;
  33. };
  34. int main(int argc, char* argv[])
  35. {
  36. using namespace boost;
  37. Graph* g;
  38. unsigned long n = 100;
  39. unsigned long n_weight = 0;
  40. unsigned long w_weight = 0;
  41. unsigned long p_weight = 0;
  42. unsigned long d = 10;
  43. long s = 0;
  44. unsigned long r = 1;
  45. char* file_name = NULL;
  46. while(--argc){
  47. if(sscanf(argv[argc],"-n%lu",&n)==1);
  48. else if(sscanf(argv[argc],"-N%lu",&n_weight)==1);
  49. else if(sscanf(argv[argc],"-W%lu",&w_weight)==1);
  50. else if(sscanf(argv[argc],"-P%lu",&p_weight)==1);
  51. else if(sscanf(argv[argc],"-d%lu",&d)==1);
  52. else if(sscanf(argv[argc],"-r%lu",&r)==1);
  53. else if(sscanf(argv[argc],"-s%ld",&s)==1);
  54. else if(strcmp(argv[argc],"-v")==0) verbose = 1;
  55. else if(strncmp(argv[argc],"-g",2)==0) file_name = argv[argc]+2;
  56. else{
  57. fprintf(stderr,
  58. "Usage: %s [-nN][-dN][-rN][-sN][-NN][-WN][-PN][-v][-gfoo]\n",
  59. argv[0]);
  60. return -2;
  61. }
  62. }
  63. if (file_name) r = 1;
  64. while (r--) {
  65. if (file_name)
  66. g = restore_graph(file_name);
  67. else
  68. g = miles(n,n_weight,w_weight,p_weight,0L,d,s);
  69. if(g == NULL || g->n <= 1) {
  70. fprintf(stderr,"Sorry, can't create the graph! (error code %ld)\n",
  71. panic_code);
  72. return-1;
  73. }
  74. printf("The graph %s has %ld edges,\n", g->id, g->m / 2);
  75. long sp_length = 0;
  76. // Use the "z" utility field for distance.
  77. typedef property_map<Graph*, z_property<long> >::type Distance;
  78. Distance d = get(z_property<long>(), g);
  79. // Use the "w" property for parent
  80. typedef property_map<Graph*, w_property<Vertex*> >::type Parent;
  81. Parent p = get(w_property<Vertex*>(), g);
  82. total_length_visitor<Distance> length_vis(sp_length, d);
  83. prim_minimum_spanning_tree(g, p,
  84. distance_map(get(z_property<long>(), g)).
  85. weight_map(get(edge_length_t(), g)).
  86. // Use the "y" utility field for color
  87. color_map(get(y_property<long>(), g)).
  88. visitor(length_vis));
  89. printf(" and its minimum spanning tree has length %ld.\n", sp_length);
  90. gb_recycle(g);
  91. s++;
  92. }
  93. return 0;
  94. }