| 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/push_relabel_max_flow.hpp> |
|---|
| 12 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 13 | #include <boost/graph/read_dimacs.hpp> |
|---|
| 14 | |
|---|
| 15 | // Use a DIMACS network flow file as stdin. |
|---|
| 16 | // push-relabel-eg < max_flow.dat |
|---|
| 17 | // |
|---|
| 18 | // Sample output: |
|---|
| 19 | // c The total flow: |
|---|
| 20 | // s 13 |
|---|
| 21 | // |
|---|
| 22 | // c flow values: |
|---|
| 23 | // f 0 6 3 |
|---|
| 24 | // f 0 1 0 |
|---|
| 25 | // f 0 2 10 |
|---|
| 26 | // f 1 5 1 |
|---|
| 27 | // f 1 0 0 |
|---|
| 28 | // f 1 3 0 |
|---|
| 29 | // f 2 4 4 |
|---|
| 30 | // f 2 3 6 |
|---|
| 31 | // f 2 0 0 |
|---|
| 32 | // f 3 7 5 |
|---|
| 33 | // f 3 2 0 |
|---|
| 34 | // f 3 1 1 |
|---|
| 35 | // f 4 5 4 |
|---|
| 36 | // f 4 6 0 |
|---|
| 37 | // f 5 4 0 |
|---|
| 38 | // f 5 7 5 |
|---|
| 39 | // f 6 7 3 |
|---|
| 40 | // f 6 4 0 |
|---|
| 41 | // f 7 6 0 |
|---|
| 42 | // f 7 5 0 |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 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 | |
|---|
| 57 | property_map < Graph, edge_capacity_t >::type |
|---|
| 58 | capacity = get(edge_capacity, g); |
|---|
| 59 | property_map < Graph, edge_residual_capacity_t >::type |
|---|
| 60 | residual_capacity = get(edge_residual_capacity, g); |
|---|
| 61 | property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); |
|---|
| 62 | Traits::vertex_descriptor s, t; |
|---|
| 63 | read_dimacs_max_flow(g, capacity, rev, s, t); |
|---|
| 64 | |
|---|
| 65 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
|---|
| 66 | property_map<Graph, vertex_index_t>::type indexmap = get(vertex_index, g); |
|---|
| 67 | long flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev, |
|---|
| 68 | indexmap); |
|---|
| 69 | #else |
|---|
| 70 | long flow = push_relabel_max_flow(g, s, t); |
|---|
| 71 | #endif |
|---|
| 72 | |
|---|
| 73 | std::cout << "c The total flow:" << std::endl; |
|---|
| 74 | std::cout << "s " << flow << std::endl << std::endl; |
|---|
| 75 | std::cout << "c flow values:" << std::endl; |
|---|
| 76 | graph_traits < Graph >::vertex_iterator u_iter, u_end; |
|---|
| 77 | graph_traits < Graph >::out_edge_iterator ei, e_end; |
|---|
| 78 | for (tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) |
|---|
| 79 | for (tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) |
|---|
| 80 | if (capacity[*ei] > 0) |
|---|
| 81 | std::cout << "f " << *u_iter << " " << target(*ei, g) << " " |
|---|
| 82 | << (capacity[*ei] - residual_capacity[*ei]) << std::endl; |
|---|
| 83 | return EXIT_SUCCESS; |
|---|
| 84 | } |
|---|