3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_MIN_COST_MAX_FLOW_H
20 #define LEMON_MIN_COST_MAX_FLOW_H
22 /// \ingroup min_cost_flow
25 /// \brief An efficient algorithm for finding a minimum cost maximum
28 #include <lemon/preflow.h>
29 #include <lemon/network_simplex.h>
30 #include <lemon/maps.h>
34 /// \addtogroup min_cost_flow
37 /// \brief An efficient algorithm for finding a minimum cost maximum
40 /// \ref lemon::MinCostFlow "MinCostMaxFlow" implements an efficient
41 /// algorithm for finding a maximum flow having minimal total cost
42 /// from a given source node to a given target node in a directed
45 /// \note \ref lemon::MinCostMaxFlow "MinCostMaxFlow" uses
46 /// \ref lemon::Preflow "Preflow" algorithm for finding the maximum
47 /// flow value and \ref lemon::NetworkSimplex "NetworkSimplex"
48 /// algorithm for finding a minimum cost flow of that value.
50 /// \param Graph The directed graph type the algorithm runs on.
51 /// \param CapacityMap The type of the capacity (upper bound) map.
52 /// \param CostMap The type of the cost (length) map.
55 /// - Edge capacities and costs should be nonnegative integers.
56 /// However \c CostMap::Value should be signed type.
58 /// \author Peter Kovacs
60 template < typename Graph,
61 typename CapacityMap = typename Graph::template EdgeMap<int>,
62 typename CostMap = typename Graph::template EdgeMap<int> >
65 typedef typename Graph::Node Node;
66 typedef typename Graph::Edge Edge;
68 typedef typename CapacityMap::Value Capacity;
69 typedef typename Graph::template NodeMap<Capacity> SupplyMap;
70 typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
76 /// \brief The type of the flow map.
77 typedef typename Graph::template EdgeMap<Capacity> FlowMap;
78 typedef typename CostMap::Value Cost;
82 /// \brief The directed graph the algorithm runs on.
84 /// \brief The modified capacity map.
85 const CapacityMap &capacity;
86 /// \brief The cost map.
88 /// \brief The source node.
90 /// \brief The target node.
92 /// \brief The edge map of the found flow.
95 typedef Preflow<Graph, CapacityMap> PreflowImpl;
96 /// \brief \ref lemon::Preflow "Preflow" class for finding the
97 /// maximum flow value.
102 /// \brief The constructor of the class.
104 /// The constructor of the class.
106 /// \param _graph The directed graph the algorithm runs on.
107 /// \param _capacity The capacities (upper bounds) of the edges.
108 /// \param _cost The cost (length) values of the edges.
109 /// \param _s The source node.
110 /// \param _t The target node.
111 MinCostMaxFlow( const Graph &_graph,
112 const CapacityMap &_capacity,
113 const CostMap &_cost,
115 graph(_graph), capacity(_capacity), cost(_cost),
116 source(_s), target(_t), flow(_graph),
117 preflow(_graph, _capacity, _s, _t)
120 /// \brief Returns a const reference to the flow map.
122 /// Returns a const reference to the flow map.
124 /// \pre \ref run() must be called before using this function.
125 const FlowMap& flowMap() const {
129 /// \brief Returns the total cost of the found flow.
131 /// Returns the total cost of the found flow. The complexity of the
132 /// function is \f$ O(e) \f$.
134 /// \pre \ref run() must be called before using this function.
135 Cost totalCost() const {
137 for (typename Graph::EdgeIt e(graph); e != INVALID; ++e)
138 c += flow[e] * cost[e];
142 /// \brief Runs the algorithm.
144 preflow.flowMap(flow);
146 MinCostFlowImpl mcf_impl( graph, capacity, cost,
147 source, target, preflow.flowValue() );
149 flow = mcf_impl.flowMap();
152 }; //class MinCostMaxFlow
158 #endif //LEMON_MIN_COST_MAX_FLOW_H