push-relabel-eg.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #ifdef _MSC_VER
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #include <boost/config.hpp>
  12. #include <iostream>
  13. #include <string>
  14. #include <boost/graph/push_relabel_max_flow.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/read_dimacs.hpp>
  17. // Use a DIMACS network flow file as stdin.
  18. // push-relabel-eg < max_flow.dat
  19. //
  20. // Sample output:
  21. // c The total flow:
  22. // s 13
  23. //
  24. // c flow values:
  25. // f 0 6 3
  26. // f 0 1 0
  27. // f 0 2 10
  28. // f 1 5 1
  29. // f 1 0 0
  30. // f 1 3 0
  31. // f 2 4 4
  32. // f 2 3 6
  33. // f 2 0 0
  34. // f 3 7 5
  35. // f 3 2 0
  36. // f 3 1 1
  37. // f 4 5 4
  38. // f 4 6 0
  39. // f 5 4 0
  40. // f 5 7 5
  41. // f 6 7 3
  42. // f 6 4 0
  43. // f 7 6 0
  44. // f 7 5 0
  45. int
  46. main()
  47. {
  48. using namespace boost;
  49. typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
  50. typedef adjacency_list < vecS, vecS, directedS,
  51. property < vertex_name_t, std::string >,
  52. property < edge_capacity_t, long,
  53. property < edge_residual_capacity_t, long,
  54. property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
  55. Graph g;
  56. property_map < Graph, edge_capacity_t >::type
  57. capacity = get(edge_capacity, g);
  58. property_map < Graph, edge_residual_capacity_t >::type
  59. residual_capacity = get(edge_residual_capacity, g);
  60. property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
  61. Traits::vertex_descriptor s, t;
  62. read_dimacs_max_flow(g, capacity, rev, s, t);
  63. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  64. property_map<Graph, vertex_index_t>::type indexmap = get(vertex_index, g);
  65. long flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev,
  66. indexmap);
  67. #else
  68. long flow = push_relabel_max_flow(g, s, t);
  69. #endif
  70. std::cout << "c The total flow:" << std::endl;
  71. std::cout << "s " << flow << std::endl << std::endl;
  72. std::cout << "c flow values:" << std::endl;
  73. graph_traits < Graph >::vertex_iterator u_iter, u_end;
  74. graph_traits < Graph >::out_edge_iterator ei, e_end;
  75. for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
  76. for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
  77. if (capacity[*ei] > 0)
  78. std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
  79. << (capacity[*ei] - residual_capacity[*ei]) << std::endl;
  80. return EXIT_SUCCESS;
  81. }