max_flow.cpp 2.7 KB

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