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