dag_shortest_paths.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <boost/graph/dag_shortest_paths.hpp>
  10. #include <boost/graph/adjacency_list.hpp>
  11. #include <iostream>
  12. // Example from Introduction to Algorithms by Cormen, et all p.537.
  13. // Sample output:
  14. // r: inifinity
  15. // s: 0
  16. // t: 2
  17. // u: 6
  18. // v: 5
  19. // x: 3
  20. int main()
  21. {
  22. using namespace boost;
  23. typedef adjacency_list<vecS, vecS, directedS,
  24. property<vertex_distance_t, int>, property<edge_weight_t, int> > graph_t;
  25. graph_t g(6);
  26. enum verts { r, s, t, u, v, x };
  27. char name[] = "rstuvx";
  28. add_edge(r, s, 5, g);
  29. add_edge(r, t, 3, g);
  30. add_edge(s, t, 2, g);
  31. add_edge(s, u, 6, g);
  32. add_edge(t, u, 7, g);
  33. add_edge(t, v, 4, g);
  34. add_edge(t, x, 2, g);
  35. add_edge(u, v, -1, g);
  36. add_edge(u, x, 1, g);
  37. add_edge(v, x, -2, g);
  38. property_map<graph_t, vertex_distance_t>::type
  39. d_map = get(vertex_distance, g);
  40. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  41. // VC++ has trouble with the named-parameter mechanism, so
  42. // we make a direct call to the underlying implementation function.
  43. std::vector<default_color_type> color(num_vertices(g));
  44. std::vector<std::size_t> pred(num_vertices(g));
  45. default_dijkstra_visitor vis;
  46. std::less<int> compare;
  47. closed_plus<int> combine;
  48. property_map<graph_t, edge_weight_t>::type w_map = get(edge_weight, g);
  49. dag_shortest_paths(g, s, d_map, w_map, &color[0], &pred[0],
  50. vis, compare, combine, (std::numeric_limits<int>::max)(), 0);
  51. #else
  52. dag_shortest_paths(g, s, distance_map(d_map));
  53. #endif
  54. graph_traits<graph_t>::vertex_iterator vi , vi_end;
  55. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  56. if (d_map[*vi] == (std::numeric_limits<int>::max)())
  57. std::cout << name[*vi] << ": inifinity\n";
  58. else
  59. std::cout << name[*vi] << ": " << d_map[*vi] << '\n';
  60. return 0;
  61. }