maelv

Untitled

Jun 4th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  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. #include <boost/config.hpp>
  9. #include <iostream>
  10. #include <string>
  11. #include <boost/graph/edmonds_karp_max_flow.hpp>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/graph/read_dimacs.hpp>
  14. #include <boost/graph/graph_utility.hpp>
  15. #include <boost/graph/find_flow_cost.hpp>
  16.  
  17. // Use a DIMACS network flow file as stdin.
  18. // edmonds-karp-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 6
  27. //  f 0 2 4
  28. //  f 1 5 1
  29. //  f 1 0 0
  30. //  f 1 3 5
  31. //  f 2 4 4
  32. //  f 2 3 0
  33. //  f 2 0 0
  34. //  f 3 7 5
  35. //  f 3 2 0
  36. //  f 3 1 0
  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.  
  46. using namespace boost;
  47.  
  48. typedef adjacency_list_traits<vecS,vecS,directedS> traits;
  49. struct edge_t {
  50.     double capacity;
  51.     float cost;
  52.     float residual_capacity;
  53.     traits::edge_descriptor reversed_edge;
  54. };
  55. struct node_t {
  56.     std::string name;
  57.     boost::default_color_type color;
  58.     traits::edge_descriptor predecessor;
  59. };
  60.  
  61. typedef adjacency_list < listS, vecS, directedS, node_t, edge_t > Graph;
  62.  
  63.  
  64. int main()
  65. {
  66.  
  67.     Graph g;
  68.     property_map < Graph, double edge_t::* >::type capacity = get(&edge_t::capacity, g);
  69.     property_map < Graph, float edge_t::* >::type cost = get(&edge_t::cost, g);
  70.     property_map < Graph, float edge_t::* >::type residual_capacity = get(&edge_t::residual_capacity, g);
  71.     property_map < Graph, traits::edge_descriptor edge_t::* >::type rev = get(&edge_t::reversed_edge, g);
  72.  
  73.     property_map < Graph, std::string node_t::* >::type name = get(&node_t::name, g);
  74.     property_map < Graph, boost::default_color_type node_t::* >::type col = get(&node_t::color, g);
  75.     property_map < Graph, traits::edge_descriptor node_t::* >::type pred = get(&node_t::predecessor, g);
  76.  
  77.  
  78.  
  79.     traits::vertex_descriptor s, t;
  80.  
  81.     read_dimacs_max_flow(g, capacity, rev, s, t);
  82.  
  83.     long flow, flow_cost;
  84.     // XXX The "non-named parameter version" (works fine)
  85.     flow = edmonds_karp_max_flow(g, s, t,capacity,residual_capacity,rev,col,pred);
  86.     // XXX The "named parameter version" (producing errors)
  87. //  flow = edmonds_karp_max_flow(g, s, t,
  88. //          capacity_map(capacity)
  89. //          .residual_capacity_map(residual_capacity)
  90. //          .reverse_edge_map(rev)
  91. //          .color_map(col)
  92. //          .predecessor_map(pred));
  93.  
  94.  
  95.     find_flow_cost(g,capacity,residual_capacity,cost);
  96.     //flow_cost = find_flow_cost(g,capacity,residual_capacity,cost);
  97.  
  98.  
  99.     std::cout << "c  The total flow:" << std::endl;
  100.     std::cout << "s " << flow << std::endl << std::endl;
  101.  
  102.     std::cout << "c flow values:" << std::endl;
  103.     graph_traits < Graph >::vertex_iterator u_iter, u_end;
  104.     graph_traits < Graph >::out_edge_iterator ei, e_end;
  105.     for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
  106.         for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
  107.             if (capacity[*ei] > 0)
  108.                 std::cout << "a " << *u_iter << " " << target(*ei, g) << " "
  109.                 << (capacity[*ei] - residual_capacity[*ei]) << std::endl;
  110.  
  111.     return EXIT_SUCCESS;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment