edmonds-karp-eg.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/edmonds_karp_max_flow.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/read_dimacs.hpp>
  17. #include <boost/graph/graph_utility.hpp>
  18. // Use a DIMACS network flow file as stdin.
  19. // edmonds-karp-eg < max_flow.dat
  20. //
  21. // Sample output:
  22. // c The total flow:
  23. // s 13
  24. //
  25. // c flow values:
  26. // f 0 6 3
  27. // f 0 1 6
  28. // f 0 2 4
  29. // f 1 5 1
  30. // f 1 0 0
  31. // f 1 3 5
  32. // f 2 4 4
  33. // f 2 3 0
  34. // f 2 0 0
  35. // f 3 7 5
  36. // f 3 2 0
  37. // f 3 1 0
  38. // f 4 5 4
  39. // f 4 6 0
  40. // f 5 4 0
  41. // f 5 7 5
  42. // f 6 7 3
  43. // f 6 4 0
  44. // f 7 6 0
  45. // f 7 5 0
  46. int
  47. main()
  48. {
  49. using namespace boost;
  50. typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
  51. typedef adjacency_list < listS, vecS, directedS,
  52. property < vertex_name_t, std::string >,
  53. property < edge_capacity_t, long,
  54. property < edge_residual_capacity_t, long,
  55. property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;
  56. Graph g;
  57. property_map < Graph, edge_capacity_t >::type
  58. capacity = get(edge_capacity, g);
  59. property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
  60. property_map < Graph, edge_residual_capacity_t >::type
  61. residual_capacity = get(edge_residual_capacity, g);
  62. Traits::vertex_descriptor s, t;
  63. read_dimacs_max_flow(g, capacity, rev, s, t);
  64. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  65. std::vector<default_color_type> color(num_vertices(g));
  66. std::vector<Traits::edge_descriptor> pred(num_vertices(g));
  67. long flow = edmonds_karp_max_flow
  68. (g, s, t, capacity, residual_capacity, rev, &color[0], &pred[0]);
  69. #else
  70. long flow = edmonds_karp_max_flow(g, s, t);
  71. #endif
  72. std::cout << "c The total flow:" << std::endl;
  73. std::cout << "s " << flow << std::endl << std::endl;
  74. std::cout << "c flow values:" << std::endl;
  75. graph_traits < Graph >::vertex_iterator u_iter, u_end;
  76. graph_traits < Graph >::out_edge_iterator ei, e_end;
  77. for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
  78. for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
  79. if (capacity[*ei] > 0)
  80. std::cout << "f " << *u_iter << " " << target(*ei, g) << " "
  81. << (capacity[*ei] - residual_capacity[*ei]) << std::endl;
  82. return EXIT_SUCCESS;
  83. }