Index: lemon/capacity_scaling.h
===================================================================
--- lemon/capacity_scaling.h	(revision 2574)
+++ lemon/capacity_scaling.h	(revision 2581)
@@ -51,9 +51,6 @@
   /// - Edge capacities and costs should be \e non-negative \e integers.
   /// - Supply values should be \e signed \e integers.
-  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
-  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
-  ///   convertible to each other.
-  /// - All value types must be convertible to \c CostMap::Value, which
-  ///   must be signed type.
+  /// - The value types of the maps should be convertible to each other.
+  /// - \c CostMap::Value must be signed type.
   ///
   /// \author Peter Kovacs
@@ -121,5 +118,5 @@
     public:
 
-      /// The constructor of the class.
+      /// Constructor.
       ResidualDijkstra( const Graph &graph,
                         const FlowMap &flow,
@@ -222,7 +219,9 @@
 
     // Edge map of the current flow
-    FlowMap _flow;
+    FlowMap *_flow;
+    bool _local_flow;
     // Node map of the current potentials
-    PotentialMap _potential;
+    PotentialMap *_potential;
+    bool _local_potential;
 
     // The residual capacity map
@@ -244,11 +243,11 @@
     // Implementation of the Dijkstra algorithm for finding augmenting
     // shortest paths in the residual network
-    ResidualDijkstra _dijkstra;
-
-  public :
-
-    /// \brief General constructor of the class (with lower bounds).
-    ///
-    /// General constructor of the class (with lower bounds).
+    ResidualDijkstra *_dijkstra;
+
+  public:
+
+    /// \brief General constructor (with lower bounds).
+    ///
+    /// General constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -263,7 +262,7 @@
                      const SupplyMap &supply ) :
       _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
-      _supply(graph), _flow(graph, 0), _potential(graph, 0),
-      _res_cap(graph), _excess(graph), _pred(graph),
-      _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred)
+      _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false),
+      _res_cap(graph), _excess(graph), _pred(graph)
     {
       // Removing non-zero lower bounds
@@ -283,7 +282,7 @@
     }
 
-    /// \brief General constructor of the class (without lower bounds).
-    ///
-    /// General constructor of the class (without lower bounds).
+    /// \brief General constructor (without lower bounds).
+    ///
+    /// General constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -296,7 +295,7 @@
                      const SupplyMap &supply ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
-      _supply(supply), _flow(graph, 0), _potential(graph, 0),
-      _res_cap(capacity), _excess(graph), _pred(graph),
-      _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred)
+      _supply(supply), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false),
+      _res_cap(capacity), _excess(graph), _pred(graph)
     {
       // Checking the sum of supply values
@@ -306,7 +305,7 @@
     }
 
-    /// \brief Simple constructor of the class (with lower bounds).
-    ///
-    /// Simple constructor of the class (with lower bounds).
+    /// \brief Simple constructor (with lower bounds).
+    ///
+    /// Simple constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -325,7 +324,7 @@
                      Supply flow_value ) :
       _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
-      _supply(graph), _flow(graph, 0), _potential(graph, 0),
-      _res_cap(graph), _excess(graph), _pred(graph),
-      _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred)
+      _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false),
+      _res_cap(graph), _excess(graph), _pred(graph)
     {
       // Removing non-zero lower bounds
@@ -345,7 +344,7 @@
     }
 
-    /// \brief Simple constructor of the class (without lower bounds).
-    ///
-    /// Simple constructor of the class (without lower bounds).
+    /// \brief Simple constructor (without lower bounds).
+    ///
+    /// Simple constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -362,7 +361,7 @@
                      Supply flow_value ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
-      _supply(graph, 0), _flow(graph, 0), _potential(graph, 0),
-      _res_cap(capacity), _excess(graph), _pred(graph),
-      _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred)
+      _supply(graph, 0), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false),
+      _res_cap(capacity), _excess(graph), _pred(graph)
     {
       _supply[s] =  flow_value;
@@ -371,4 +370,45 @@
     }
 
+    /// Destructor.
+    ~CapacityScaling() {
+      if (_local_flow) delete _flow;
+      if (_local_potential) delete _potential;
+      delete _dijkstra;
+    }
+
+    /// \brief Sets the flow map.
+    ///
+    /// Sets the flow map.
+    ///
+    /// \return \c (*this)
+    CapacityScaling& flowMap(FlowMap &map) {
+      if (_local_flow) {
+        delete _flow;
+        _local_flow = false;
+      }
+      _flow = &map;
+      return *this;
+    }
+
+    /// \brief Sets the potential map.
+    ///
+    /// Sets the potential map.
+    ///
+    /// \return \c (*this)
+    CapacityScaling& potentialMap(PotentialMap &map) {
+      if (_local_potential) {
+        delete _potential;
+        _local_potential = false;
+      }
+      _potential = &map;
+      return *this;
+    }
+
+    /// \name Execution control
+    /// The only way to execute the algorithm is to call the run()
+    /// function.
+
+    /// @{
+
     /// \brief Runs the algorithm.
     ///
@@ -385,4 +425,13 @@
     }
 
+    /// @}
+
+    /// \name Query Functions
+    /// The result of the algorithm can be obtained using these
+    /// functions.
+    /// \n run() must be called before using them.
+
+    /// @{
+
     /// \brief Returns a const reference to the edge map storing the
     /// found flow.
@@ -392,5 +441,5 @@
     /// \pre \ref run() must be called before using this function.
     const FlowMap& flowMap() const {
-      return _flow;
+      return *_flow;
     }
 
@@ -403,5 +452,23 @@
     /// \pre \ref run() must be called before using this function.
     const PotentialMap& potentialMap() const {
-      return _potential;
+      return *_potential;
+    }
+
+    /// \brief Returns the flow on the edge.
+    ///
+    /// Returns the flow on the edge.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Capacity flow(const Edge& edge) const {
+      return (*_flow)[edge];
+    }
+
+    /// \brief Returns the potential of the node.
+    ///
+    /// Returns the potential of the node.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Cost potential(const Node& node) const {
+      return (*_potential)[node];
     }
 
@@ -415,7 +482,9 @@
       Cost c = 0;
       for (EdgeIt e(_graph); e != INVALID; ++e)
-        c += _flow[e] * _cost[e];
+        c += (*_flow)[e] * _cost[e];
       return c;
     }
+
+    /// @}
 
   private:
@@ -424,7 +493,22 @@
     bool init(bool scaling) {
       if (!_valid_supply) return false;
+
+      // Initializing maps
+      if (!_flow) {
+        _flow = new FlowMap(_graph);
+        _local_flow = true;
+      }
+      if (!_potential) {
+        _potential = new PotentialMap(_graph);
+        _local_potential = true;
+      }
+      for (EdgeIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
+      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
       _excess = _supply;
 
-      // Initilaizing delta value
+      _dijkstra = new ResidualDijkstra( _graph, *_flow, _res_cap, _cost,
+                                        _excess, *_potential, _pred );
+
+      // Initializing delta value
       if (scaling) {
         // With scaling
@@ -442,4 +526,5 @@
         _delta = 1;
       }
+
       return true;
     }
@@ -462,15 +547,15 @@
         for (EdgeIt e(_graph); e != INVALID; ++e) {
           Node u = _graph.source(e), v = _graph.target(e);
-          Cost c = _cost[e] + _potential[u] - _potential[v];
+          Cost c = _cost[e] + (*_potential)[u] - (*_potential)[v];
           if (c < 0 && _res_cap[e] >= _delta) {
             _excess[u] -= _res_cap[e];
             _excess[v] += _res_cap[e];
-            _flow[e] = _capacity[e];
+            (*_flow)[e] = _capacity[e];
             _res_cap[e] = 0;
           }
-          else if (c > 0 && _flow[e] >= _delta) {
-            _excess[u] += _flow[e];
-            _excess[v] -= _flow[e];
-            _flow[e] = 0;
+          else if (c > 0 && (*_flow)[e] >= _delta) {
+            _excess[u] += (*_flow)[e];
+            _excess[v] -= (*_flow)[e];
+            (*_flow)[e] = 0;
             _res_cap[e] = _capacity[e];
           }
@@ -502,5 +587,5 @@
           // Running Dijkstra
           s = _excess_nodes[next_node];
-          if ((t = _dijkstra.run(s, _delta)) == INVALID) {
+          if ((t = _dijkstra->run(s, _delta)) == INVALID) {
             if (_delta > 1) {
               ++next_node;
@@ -521,5 +606,5 @@
                 u = _graph.source(e);
               } else {
-                rc = _flow[e];
+                rc = (*_flow)[e];
                 u = _graph.target(e);
               }
@@ -530,9 +615,9 @@
           while ((e = _pred[u]) != INVALID) {
             if (u == _graph.target(e)) {
-              _flow[e] += d;
+              (*_flow)[e] += d;
               _res_cap[e] -= d;
               u = _graph.source(e);
             } else {
-              _flow[e] -= d;
+              (*_flow)[e] -= d;
               _res_cap[e] += d;
               u = _graph.target(e);
@@ -553,5 +638,5 @@
       if (_lower) {
         for (EdgeIt e(_graph); e != INVALID; ++e)
-          _flow[e] += (*_lower)[e];
+          (*_flow)[e] += (*_lower)[e];
       }
       return true;
@@ -573,5 +658,5 @@
         // Running Dijkstra
         s = _excess_nodes[next_node];
-        if ((t = _dijkstra.run(s, 1)) == INVALID)
+        if ((t = _dijkstra->run(s, 1)) == INVALID)
           return false;
 
@@ -586,5 +671,5 @@
             u = _graph.source(e);
           } else {
-            rc = _flow[e];
+            rc = (*_flow)[e];
             u = _graph.target(e);
           }
@@ -594,9 +679,9 @@
         while ((e = _pred[u]) != INVALID) {
           if (u == _graph.target(e)) {
-            _flow[e] += d;
+            (*_flow)[e] += d;
             _res_cap[e] -= d;
             u = _graph.source(e);
           } else {
-            _flow[e] -= d;
+            (*_flow)[e] -= d;
             _res_cap[e] += d;
             u = _graph.target(e);
@@ -610,5 +695,5 @@
       if (_lower) {
         for (EdgeIt e(_graph); e != INVALID; ++e)
-          _flow[e] += (*_lower)[e];
+          (*_flow)[e] += (*_lower)[e];
       }
       return true;
Index: lemon/cost_scaling.h
===================================================================
--- lemon/cost_scaling.h	(revision 2577)
+++ lemon/cost_scaling.h	(revision 2581)
@@ -55,9 +55,6 @@
   /// - Edge capacities and costs should be \e non-negative \e integers.
   /// - Supply values should be \e signed \e integers.
-  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
-  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
-  ///   convertible to each other.
-  /// - All value types must be convertible to \c CostMap::Value, which
-  ///   must be signed type.
+  /// - The value types of the maps should be convertible to each other.
+  /// - \c CostMap::Value must be signed type.
   ///
   /// \note Edge costs are multiplied with the number of nodes during
@@ -98,5 +95,5 @@
 
     /// The type of the flow map.
-    typedef CapacityEdgeMap FlowMap;
+    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
     /// The type of the potential map.
     typedef typename Graph::template NodeMap<LCost> PotentialMap;
@@ -108,18 +105,19 @@
     /// \ref ResidualCostMap is a map adaptor class for handling
     /// residual edge costs.
-    class ResidualCostMap : public MapBase<ResEdge, LCost>
+    template <typename Map>
+    class ResidualCostMap : public MapBase<ResEdge, typename Map::Value>
     {
     private:
 
-      const LargeCostMap &_cost_map;
+      const Map &_cost_map;
 
     public:
 
       ///\e
-      ResidualCostMap(const LargeCostMap &cost_map) :
+      ResidualCostMap(const Map &cost_map) :
         _cost_map(cost_map) {}
 
       ///\e
-      LCost operator[](const ResEdge &e) const {
+      typename Map::Value operator[](const ResEdge &e) const {
         return ResGraph::forward(e) ?  _cost_map[e] : -_cost_map[e];
       }
@@ -161,6 +159,6 @@
 
     // Paramters for heuristics
-    static const int BF_HEURISTIC_EPSILON_BOUND    = 5000;
-    static const int BF_HEURISTIC_BOUND_FACTOR = 3;
+    static const int BF_HEURISTIC_EPSILON_BOUND = 5000;
+    static const int BF_HEURISTIC_BOUND_FACTOR  = 3;
 
   private:
@@ -181,14 +179,16 @@
 
     // Edge map of the current flow
-    FlowMap _flow;
+    FlowMap *_flow;
+    bool _local_flow;
     // Node map of the current potentials
-    PotentialMap _potential;
+    PotentialMap *_potential;
+    bool _local_potential;
 
     // The residual graph
-    ResGraph _res_graph;
+    ResGraph *_res_graph;
     // The residual cost map
-    ResidualCostMap _res_cost;
+    ResidualCostMap<LargeCostMap> _res_cost;
     // The reduced cost map
-    ReducedCostMap _red_cost;
+    ReducedCostMap *_red_cost;
     // The excess map
     SupplyNodeMap _excess;
@@ -198,7 +198,7 @@
   public:
 
-    /// \brief General constructor of the class (with lower bounds).
-    ///
-    /// General constructor of the class (with lower bounds).
+    /// \brief General constructor (with lower bounds).
+    ///
+    /// General constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -213,7 +213,7 @@
                  const SupplyMap &supply ) :
       _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
-      _cost(graph), _supply(graph), _flow(graph, 0), _potential(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
-      _red_cost(graph, _cost, _potential), _excess(graph, 0)
+      _cost(graph), _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost),
+      _excess(graph, 0)
     {
       // Removing non-zero lower bounds
@@ -232,7 +232,7 @@
     }
 
-    /// \brief General constructor of the class (without lower bounds).
-    ///
-    /// General constructor of the class (without lower bounds).
+    /// \brief General constructor (without lower bounds).
+    ///
+    /// General constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -245,7 +245,7 @@
                  const SupplyMap &supply ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
-      _cost(graph), _supply(supply), _flow(graph, 0), _potential(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
-      _red_cost(graph, _cost, _potential), _excess(graph, 0)
+      _cost(graph), _supply(supply), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost),
+      _excess(graph, 0)
     {
       // Checking the sum of supply values
@@ -255,7 +255,7 @@
     }
 
-    /// \brief Simple constructor of the class (with lower bounds).
-    ///
-    /// Simple constructor of the class (with lower bounds).
+    /// \brief Simple constructor (with lower bounds).
+    ///
+    /// Simple constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -274,7 +274,7 @@
                  Supply flow_value ) :
       _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
-      _cost(graph), _supply(graph), _flow(graph, 0), _potential(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
-      _red_cost(graph, _cost, _potential), _excess(graph, 0)
+      _cost(graph), _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost),
+      _excess(graph, 0)
     {
       // Removing nonzero lower bounds
@@ -293,7 +293,7 @@
     }
 
-    /// \brief Simple constructor of the class (without lower bounds).
-    ///
-    /// Simple constructor of the class (without lower bounds).
+    /// \brief Simple constructor (without lower bounds).
+    ///
+    /// Simple constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -310,7 +310,7 @@
                  Supply flow_value ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
-      _cost(graph), _supply(graph, 0), _flow(graph, 0), _potential(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
-      _red_cost(graph, _cost, _potential), _excess(graph, 0)
+      _cost(graph), _supply(graph, 0), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost),
+      _excess(graph, 0)
     {
       _supply[s] =  flow_value;
@@ -319,4 +319,46 @@
     }
 
+    /// Destructor.
+    ~CostScaling() {
+      if (_local_flow) delete _flow;
+      if (_local_potential) delete _potential;
+      delete _res_graph;
+      delete _red_cost;
+    }
+
+    /// \brief Sets the flow map.
+    ///
+    /// Sets the flow map.
+    ///
+    /// \return \c (*this)
+    CostScaling& flowMap(FlowMap &map) {
+      if (_local_flow) {
+        delete _flow;
+        _local_flow = false;
+      }
+      _flow = &map;
+      return *this;
+    }
+
+    /// \brief Sets the potential map.
+    ///
+    /// Sets the potential map.
+    ///
+    /// \return \c (*this)
+    CostScaling& potentialMap(PotentialMap &map) {
+      if (_local_potential) {
+        delete _potential;
+        _local_potential = false;
+      }
+      _potential = &map;
+      return *this;
+    }
+
+    /// \name Execution control
+    /// The only way to execute the algorithm is to call the run()
+    /// function.
+
+    /// @{
+
     /// \brief Runs the algorithm.
     ///
@@ -325,6 +367,15 @@
     /// \return \c true if a feasible flow can be found.
     bool run() {
-      init() && start();
-    }
+      return init() && start();
+    }
+
+    /// @}
+
+    /// \name Query Functions
+    /// The result of the algorithm can be obtained using these
+    /// functions.
+    /// \n run() must be called before using them.
+
+    /// @{
 
     /// \brief Returns a const reference to the edge map storing the
@@ -335,5 +386,5 @@
     /// \pre \ref run() must be called before using this function.
     const FlowMap& flowMap() const {
-      return _flow;
+      return *_flow;
     }
 
@@ -346,5 +397,23 @@
     /// \pre \ref run() must be called before using this function.
     const PotentialMap& potentialMap() const {
-      return _potential;
+      return *_potential;
+    }
+
+    /// \brief Returns the flow on the edge.
+    ///
+    /// Returns the flow on the edge.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Capacity flow(const Edge& edge) const {
+      return (*_flow)[edge];
+    }
+
+    /// \brief Returns the potential of the node.
+    ///
+    /// Returns the potential of the node.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Cost potential(const Node& node) const {
+      return (*_potential)[node];
     }
 
@@ -358,7 +427,9 @@
       Cost c = 0;
       for (EdgeIt e(_graph); e != INVALID; ++e)
-        c += _flow[e] * _orig_cost[e];
+        c += (*_flow)[e] * _orig_cost[e];
       return c;
     }
+
+    /// @}
 
   private:
@@ -367,4 +438,17 @@
     bool init() {
       if (!_valid_supply) return false;
+
+      // Initializing flow and potential maps
+      if (!_flow) {
+        _flow = new FlowMap(_graph);
+        _local_flow = true;
+      }
+      if (!_potential) {
+        _potential = new PotentialMap(_graph);
+        _local_potential = true;
+      }
+
+      _red_cost = new ReducedCostMap(_graph, _cost, *_potential);
+      _res_graph = new ResGraph(_graph, _capacity, *_flow);
 
       // Initializing the scaled cost map and the epsilon parameter
@@ -380,7 +464,7 @@
       Circulation< Graph, ConstMap<Edge, Capacity>, CapacityEdgeMap,
                    SupplyMap >
-        circulation( _graph, constMap<Edge>((Capacity)0), _capacity,
+        circulation( _graph, constMap<Edge>(Capacity(0)), _capacity,
                      _supply );
-      return circulation.flowMap(_flow).run();
+      return circulation.flowMap(*_flow).run();
     }
 
@@ -398,7 +482,7 @@
         // algorithm
         if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
-          typedef ShiftMap<ResidualCostMap> ShiftCostMap;
+          typedef ShiftMap< ResidualCostMap<LargeCostMap> > ShiftCostMap;
           ShiftCostMap shift_cost(_res_cost, _epsilon);
-          BellmanFord<ResGraph, ShiftCostMap> bf(_res_graph, shift_cost);
+          BellmanFord<ResGraph, ShiftCostMap> bf(*_res_graph, shift_cost);
           bf.init(0);
           bool done = false;
@@ -408,5 +492,5 @@
           if (done) {
             for (NodeIt n(_graph); n != INVALID; ++n)
-              _potential[n] = bf.dist(n);
+              (*_potential)[n] = bf.dist(n);
             continue;
           }
@@ -416,14 +500,14 @@
         Capacity delta;
         for (EdgeIt e(_graph); e != INVALID; ++e) {
-          if (_capacity[e] - _flow[e] > 0 && _red_cost[e] < 0) {
-            delta = _capacity[e] - _flow[e];
+          if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
+            delta = _capacity[e] - (*_flow)[e];
             _excess[_graph.source(e)] -= delta;
             _excess[_graph.target(e)] += delta;
-            _flow[e] = _capacity[e];
+            (*_flow)[e] = _capacity[e];
           }
-          if (_flow[e] > 0 && -_red_cost[e] < 0) {
-            _excess[_graph.target(e)] -= _flow[e];
-            _excess[_graph.source(e)] += _flow[e];
-            _flow[e] = 0;
+          if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
+            _excess[_graph.target(e)] -= (*_flow)[e];
+            _excess[_graph.source(e)] += (*_flow)[e];
+            (*_flow)[e] = 0;
           }
         }
@@ -441,7 +525,7 @@
           if (_excess[n] > 0) {
             for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
-              if (_capacity[e] - _flow[e] > 0 && _red_cost[e] < 0) {
-                delta = _capacity[e] - _flow[e] <= _excess[n] ?
-                        _capacity[e] - _flow[e] : _excess[n];
+              if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
+                delta = _capacity[e] - (*_flow)[e] <= _excess[n] ?
+                        _capacity[e] - (*_flow)[e] : _excess[n];
                 t = _graph.target(e);
 
@@ -449,10 +533,10 @@
                 Capacity ahead = -_excess[t];
                 for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
-                  if (_capacity[oe] - _flow[oe] > 0 && _red_cost[oe] < 0)
-                    ahead += _capacity[oe] - _flow[oe];
+                  if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
+                    ahead += _capacity[oe] - (*_flow)[oe];
                 }
                 for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
-                  if (_flow[ie] > 0 && -_red_cost[ie] < 0)
-                    ahead += _flow[ie];
+                  if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
+                    ahead += (*_flow)[ie];
                 }
                 if (ahead < 0) ahead = 0;
@@ -460,5 +544,5 @@
                 // Pushing flow along the edge
                 if (ahead < delta) {
-                  _flow[e] += ahead;
+                  (*_flow)[e] += ahead;
                   _excess[n] -= ahead;
                   _excess[t] += ahead;
@@ -468,5 +552,5 @@
                   break;
                 } else {
-                  _flow[e] += delta;
+                  (*_flow)[e] += delta;
                   _excess[n] -= delta;
                   _excess[t] += delta;
@@ -482,6 +566,6 @@
           if (_excess[n] > 0) {
             for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
-              if (_flow[e] > 0 && -_red_cost[e] < 0) {
-                delta = _flow[e] <= _excess[n] ? _flow[e] : _excess[n];
+              if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
+                delta = (*_flow)[e] <= _excess[n] ? (*_flow)[e] : _excess[n];
                 t = _graph.source(e);
 
@@ -489,10 +573,10 @@
                 Capacity ahead = -_excess[t];
                 for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
-                  if (_capacity[oe] - _flow[oe] > 0 && _red_cost[oe] < 0)
-                    ahead += _capacity[oe] - _flow[oe];
+                  if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
+                    ahead += _capacity[oe] - (*_flow)[oe];
                 }
                 for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
-                  if (_flow[ie] > 0 && -_red_cost[ie] < 0)
-                    ahead += _flow[ie];
+                  if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
+                    ahead += (*_flow)[ie];
                 }
                 if (ahead < 0) ahead = 0;
@@ -500,5 +584,5 @@
                 // Pushing flow along the edge
                 if (ahead < delta) {
-                  _flow[e] -= ahead;
+                  (*_flow)[e] -= ahead;
                   _excess[n] -= ahead;
                   _excess[t] += ahead;
@@ -508,5 +592,5 @@
                   break;
                 } else {
-                  _flow[e] -= delta;
+                  (*_flow)[e] -= delta;
                   _excess[n] -= delta;
                   _excess[t] += delta;
@@ -524,13 +608,13 @@
             LCost min_red_cost = std::numeric_limits<LCost>::max();
             for (OutEdgeIt oe(_graph, n); oe != INVALID; ++oe) {
-              if ( _capacity[oe] - _flow[oe] > 0 &&
-                   _red_cost[oe] < min_red_cost )
-                min_red_cost = _red_cost[oe];
+              if ( _capacity[oe] - (*_flow)[oe] > 0 &&
+                   (*_red_cost)[oe] < min_red_cost )
+                min_red_cost = (*_red_cost)[oe];
             }
             for (InEdgeIt ie(_graph, n); ie != INVALID; ++ie) {
-              if (_flow[ie] > 0 && -_red_cost[ie] < min_red_cost)
-                min_red_cost = -_red_cost[ie];
+              if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < min_red_cost)
+                min_red_cost = -(*_red_cost)[ie];
             }
-            _potential[n] -= min_red_cost + _epsilon;
+            (*_potential)[n] -= min_red_cost + _epsilon;
             hyper[n] = false;
           }
@@ -545,8 +629,16 @@
       }
 
+      // Computing node potentials for the original costs
+      ResidualCostMap<CostMap> res_cost(_orig_cost);
+      BellmanFord< ResGraph, ResidualCostMap<CostMap> >
+        bf(*_res_graph, res_cost);
+      bf.init(0); bf.start();
+      for (NodeIt n(_graph); n != INVALID; ++n)
+        (*_potential)[n] = bf.dist(n);
+
       // Handling non-zero lower bounds
       if (_lower) {
         for (EdgeIt e(_graph); e != INVALID; ++e)
-          _flow[e] += (*_lower)[e];
+          (*_flow)[e] += (*_lower)[e];
       }
       return true;
Index: lemon/cycle_canceling.h
===================================================================
--- lemon/cycle_canceling.h	(revision 2573)
+++ lemon/cycle_canceling.h	(revision 2581)
@@ -53,9 +53,6 @@
   /// - Edge capacities and costs should be \e non-negative \e integers.
   /// - Supply values should be \e signed \e integers.
-  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
-  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
-  ///   convertible to each other.
-  /// - All value types must be convertible to \c CostMap::Value, which
-  ///   must be signed type.
+  /// - The value types of the maps should be convertible to each other.
+  /// - \c CostMap::Value must be signed type.
   ///
   /// \note By default the \ref BellmanFord "Bellman-Ford" algorithm is
@@ -95,4 +92,6 @@
     /// The type of the flow map.
     typedef typename Graph::template EdgeMap<Capacity> FlowMap;
+    /// The type of the potential map.
+    typedef typename Graph::template NodeMap<Cost> PotentialMap;
 
   private:
@@ -144,8 +143,12 @@
 
     // Edge map of the current flow
-    FlowMap _flow;
+    FlowMap *_flow;
+    bool _local_flow;
+    // Node map of the current potentials
+    PotentialMap *_potential;
+    bool _local_potential;
 
     // The residual graph
-    ResGraph _res_graph;
+    ResGraph *_res_graph;
     // The residual cost map
     ResidualCostMap _res_cost;
@@ -153,7 +156,7 @@
   public:
 
-    /// \brief General constructor of the class (with lower bounds).
-    ///
-    /// General constructor of the class (with lower bounds).
+    /// \brief General constructor (with lower bounds).
+    ///
+    /// General constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -168,6 +171,6 @@
                     const SupplyMap &supply ) :
       _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
-      _supply(graph), _flow(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost)
+      _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost)
     {
       // Removing non-zero lower bounds
@@ -185,7 +188,7 @@
     }
 
-    /// \brief General constructor of the class (without lower bounds).
-    ///
-    /// General constructor of the class (without lower bounds).
+    /// \brief General constructor (without lower bounds).
+    ///
+    /// General constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -198,6 +201,6 @@
                     const SupplyMap &supply ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
-      _supply(supply), _flow(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost)
+      _supply(supply), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost)
     {
       // Checking the sum of supply values
@@ -207,7 +210,7 @@
     }
 
-    /// \brief Simple constructor of the class (with lower bounds).
-    ///
-    /// Simple constructor of the class (with lower bounds).
+    /// \brief Simple constructor (with lower bounds).
+    ///
+    /// Simple constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -226,6 +229,6 @@
                     Supply flow_value ) :
       _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
-      _supply(graph), _flow(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost)
+      _supply(graph), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost)
     {
       // Removing non-zero lower bounds
@@ -244,7 +247,7 @@
     }
 
-    /// \brief Simple constructor of the class (without lower bounds).
-    ///
-    /// Simple constructor of the class (without lower bounds).
+    /// \brief Simple constructor (without lower bounds).
+    ///
+    /// Simple constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -261,6 +264,6 @@
                     Supply flow_value ) :
       _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
-      _supply(graph, 0), _flow(graph, 0),
-      _res_graph(graph, _capacity, _flow), _res_cost(_cost)
+      _supply(graph, 0), _flow(0), _local_flow(false),
+      _potential(0), _local_potential(false), _res_cost(_cost)
     {
       _supply[s] =  flow_value;
@@ -269,4 +272,45 @@
     }
 
+    /// Destructor.
+    ~CycleCanceling() {
+      if (_local_flow) delete _flow;
+      if (_local_potential) delete _potential;
+      delete _res_graph;
+    }
+
+    /// \brief Sets the flow map.
+    ///
+    /// Sets the flow map.
+    ///
+    /// \return \c (*this)
+    CycleCanceling& flowMap(FlowMap &map) {
+      if (_local_flow) {
+        delete _flow;
+        _local_flow = false;
+      }
+      _flow = &map;
+      return *this;
+    }
+
+    /// \brief Sets the potential map.
+    ///
+    /// Sets the potential map.
+    ///
+    /// \return \c (*this)
+    CycleCanceling& potentialMap(PotentialMap &map) {
+      if (_local_potential) {
+        delete _potential;
+        _local_potential = false;
+      }
+      _potential = &map;
+      return *this;
+    }
+
+    /// \name Execution control
+    /// The only way to execute the algorithm is to call the run()
+    /// function.
+
+    /// @{
+
     /// \brief Runs the algorithm.
     ///
@@ -282,4 +326,13 @@
     }
 
+    /// @}
+
+    /// \name Query Functions
+    /// The result of the algorithm can be obtained using these
+    /// functions.
+    /// \n run() must be called before using them.
+
+    /// @{
+
     /// \brief Returns a const reference to the edge map storing the
     /// found flow.
@@ -289,5 +342,34 @@
     /// \pre \ref run() must be called before using this function.
     const FlowMap& flowMap() const {
-      return _flow;
+      return *_flow;
+    }
+
+    /// \brief Returns a const reference to the node map storing the
+    /// found potentials (the dual solution).
+    ///
+    /// Returns a const reference to the node map storing the found
+    /// potentials (the dual solution).
+    ///
+    /// \pre \ref run() must be called before using this function.
+    const PotentialMap& potentialMap() const {
+      return *_potential;
+    }
+
+    /// \brief Returns the flow on the edge.
+    ///
+    /// Returns the flow on the edge.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Capacity flow(const Edge& edge) const {
+      return (*_flow)[edge];
+    }
+
+    /// \brief Returns the potential of the node.
+    ///
+    /// Returns the potential of the node.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Cost potential(const Node& node) const {
+      return (*_potential)[node];
     }
 
@@ -301,7 +383,9 @@
       Cost c = 0;
       for (EdgeIt e(_graph); e != INVALID; ++e)
-        c += _flow[e] * _cost[e];
+        c += (*_flow)[e] * _cost[e];
       return c;
     }
+
+    /// @}
 
   private:
@@ -311,17 +395,36 @@
       if (!_valid_supply) return false;
 
+      // Initializing flow and potential maps
+      if (!_flow) {
+        _flow = new FlowMap(_graph);
+        _local_flow = true;
+      }
+      if (!_potential) {
+        _potential = new PotentialMap(_graph);
+        _local_potential = true;
+      }
+
+      _res_graph = new ResGraph(_graph, _capacity, *_flow);
+
       // Finding a feasible flow using Circulation
       Circulation< Graph, ConstMap<Edge, Capacity>, CapacityEdgeMap,
                    SupplyMap >
-        circulation( _graph, constMap<Edge>((Capacity)0), _capacity,
+        circulation( _graph, constMap<Edge>(Capacity(0)), _capacity,
                      _supply );
-      return circulation.flowMap(_flow).run();
+      return circulation.flowMap(*_flow).run();
     }
 
     bool start(bool min_mean_cc) {
       if (min_mean_cc)
-        return startMinMean();
+        startMinMean();
       else
-        return start();
+        start();
+
+      // Handling non-zero lower bounds
+      if (_lower) {
+        for (EdgeIt e(_graph); e != INVALID; ++e)
+          (*_flow)[e] += (*_lower)[e];
+      }
+      return true;
     }
 
@@ -331,7 +434,7 @@
     /// "Bellman-Ford" algorithm for negative cycle detection with
     /// successively larger limit for the number of iterations.
-    bool start() {
-      typename BellmanFord<ResGraph, ResidualCostMap>::PredMap pred(_res_graph);
-      typename ResGraph::template NodeMap<int> visited(_res_graph);
+    void start() {
+      typename BellmanFord<ResGraph, ResidualCostMap>::PredMap pred(*_res_graph);
+      typename ResGraph::template NodeMap<int> visited(*_res_graph);
       std::vector<ResEdge> cycle;
       int node_num = countNodes(_graph);
@@ -340,5 +443,5 @@
       bool optimal = false;
       while (!optimal) {
-        BellmanFord<ResGraph, ResidualCostMap> bf(_res_graph, _res_cost);
+        BellmanFord<ResGraph, ResidualCostMap> bf(*_res_graph, _res_cost);
         bf.predMap(pred);
         bf.init(0);
@@ -357,20 +460,24 @@
           }
           if (real_iter_num < curr_iter_num) {
+            // Optimal flow is found
             optimal = true;
+            // Setting node potentials
+            for (NodeIt n(_graph); n != INVALID; ++n)
+              (*_potential)[n] = bf.dist(n);
             break;
           } else {
             // Searching for node disjoint negative cycles
-            for (ResNodeIt n(_res_graph); n != INVALID; ++n)
+            for (ResNodeIt n(*_res_graph); n != INVALID; ++n)
               visited[n] = 0;
             int id = 0;
-            for (ResNodeIt n(_res_graph); n != INVALID; ++n) {
+            for (ResNodeIt n(*_res_graph); n != INVALID; ++n) {
               if (visited[n] > 0) continue;
               visited[n] = ++id;
               ResNode u = pred[n] == INVALID ?
-                          INVALID : _res_graph.source(pred[n]);
+                          INVALID : _res_graph->source(pred[n]);
               while (u != INVALID && visited[u] == 0) {
                 visited[u] = id;
                 u = pred[u] == INVALID ?
-                    INVALID : _res_graph.source(pred[u]);
+                    INVALID : _res_graph->source(pred[u]);
               }
               if (u != INVALID && visited[u] == id) {
@@ -380,14 +487,14 @@
                 ResEdge e = pred[u];
                 cycle.push_back(e);
-                Capacity d = _res_graph.rescap(e);
-                while (_res_graph.source(e) != u) {
-                  cycle.push_back(e = pred[_res_graph.source(e)]);
-                  if (_res_graph.rescap(e) < d)
-                    d = _res_graph.rescap(e);
+                Capacity d = _res_graph->rescap(e);
+                while (_res_graph->source(e) != u) {
+                  cycle.push_back(e = pred[_res_graph->source(e)]);
+                  if (_res_graph->rescap(e) < d)
+                    d = _res_graph->rescap(e);
                 }
 
                 // Augmenting along the cycle
                 for (int i = 0; i < int(cycle.size()); ++i)
-                  _res_graph.augment(cycle[i], d);
+                  _res_graph->augment(cycle[i], d);
               }
             }
@@ -398,11 +505,4 @@
         }
       }
-
-      // Handling non-zero lower bounds
-      if (_lower) {
-        for (EdgeIt e(_graph); e != INVALID; ++e)
-          _flow[e] += (*_lower)[e];
-      }
-      return true;
     }
 
@@ -411,7 +511,7 @@
     /// Executes the algorithm using \ref MinMeanCycle for negative
     /// cycle detection.
-    bool startMinMean() {
+    void startMinMean() {
       typedef Path<ResGraph> ResPath;
-      MinMeanCycle<ResGraph, ResidualCostMap> mmc(_res_graph, _res_cost);
+      MinMeanCycle<ResGraph, ResidualCostMap> mmc(*_res_graph, _res_cost);
       ResPath cycle;
 
@@ -426,11 +526,11 @@
           Capacity delta = 0;
           for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e) {
-            if (delta == 0 || _res_graph.rescap(e) < delta)
-              delta = _res_graph.rescap(e);
+            if (delta == 0 || _res_graph->rescap(e) < delta)
+              delta = _res_graph->rescap(e);
           }
 
           // Augmenting along the cycle
           for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e)
-            _res_graph.augment(e, delta);
+            _res_graph->augment(e, delta);
 
           // Finding the minimum cycle mean for the modified residual
@@ -441,10 +541,9 @@
       }
 
-      // Handling non-zero lower bounds
-      if (_lower) {
-        for (EdgeIt e(_graph); e != INVALID; ++e)
-          _flow[e] += (*_lower)[e];
-      }
-      return true;
+      // Computing node potentials
+      BellmanFord<ResGraph, ResidualCostMap> bf(*_res_graph, _res_cost);
+      bf.init(0); bf.start();
+      for (NodeIt n(_graph); n != INVALID; ++n)
+        (*_potential)[n] = bf.dist(n);
     }
 
Index: lemon/min_cost_flow.h
===================================================================
--- lemon/min_cost_flow.h	(revision 2579)
+++ lemon/min_cost_flow.h	(revision 2581)
@@ -44,7 +44,5 @@
   ///
   /// There are four implementations for the minimum cost flow problem,
-  /// which can be used exactly the same way except for the fact that
-  /// \ref CycleCanceling does not provide dual solution (node
-  /// potentials) since it is a pure primal method.
+  /// which can be used exactly the same way.
   /// - \ref CapacityScaling The capacity scaling algorithm.
   /// - \ref CostScaling The cost scaling algorithm.
@@ -61,9 +59,6 @@
   /// - Edge capacities and costs should be \e non-negative \e integers.
   /// - Supply values should be \e signed \e integers.
-  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
-  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
-  ///   convertible to each other.
-  /// - All value types must be convertible to \c CostMap::Value, which
-  ///   must be signed type.
+  /// - The value types of the maps should be convertible to each other.
+  /// - \c CostMap::Value must be signed type.
   ///
   /// \author Peter Kovacs
@@ -71,8 +66,7 @@
   template < typename Graph,
              typename LowerMap = typename Graph::template EdgeMap<int>,
-             typename CapacityMap = LowerMap,
+             typename CapacityMap = typename Graph::template EdgeMap<int>,
              typename CostMap = typename Graph::template EdgeMap<int>,
-             typename SupplyMap = typename Graph::template NodeMap
-                                  <typename CapacityMap::Value> >
+             typename SupplyMap = typename Graph::template NodeMap<int> >
   class MinCostFlow :
     public NetworkSimplex< Graph, LowerMap, CapacityMap,
@@ -86,5 +80,5 @@
   public:
 
-    /// General constructor of the class (with lower bounds).
+    /// General constructor (with lower bounds).
     MinCostFlow( const Graph &graph,
                  const LowerMap &lower,
@@ -101,5 +95,5 @@
       MinCostFlowImpl(graph, capacity, cost, supply) {}
 
-    /// Simple constructor of the class (with lower bounds).
+    /// Simple constructor (with lower bounds).
     MinCostFlow( const Graph &graph,
                  const LowerMap &lower,
@@ -111,5 +105,5 @@
                        s, t, flow_value ) {}
 
-    /// Simple constructor of the class (without lower bounds).
+    /// Simple constructor (without lower bounds).
     MinCostFlow( const Graph &graph,
                  const CapacityMap &capacity,
Index: lemon/network_simplex.h
===================================================================
--- lemon/network_simplex.h	(revision 2579)
+++ lemon/network_simplex.h	(revision 2581)
@@ -53,9 +53,6 @@
   /// - Edge capacities and costs should be \e non-negative \e integers.
   /// - Supply values should be \e signed \e integers.
-  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
-  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
-  ///   convertible to each other.
-  /// - All value types must be convertible to \c CostMap::Value, which
-  ///   must be signed type.
+  /// - The value types of the maps should be convertible to each other.
+  /// - \c CostMap::Value must be signed type.
   ///
   /// \note \ref NetworkSimplex provides six different pivot rule
@@ -470,6 +467,8 @@
 
     // Members for handling the original graph
-    FlowMap _flow_result;
-    PotentialMap _potential_result;
+    FlowMap *_flow_result;
+    PotentialMap *_potential_result;
+    bool _local_flow;
+    bool _local_potential;
     NodeRefMap _node_ref;
     EdgeRefMap _edge_ref;
@@ -488,7 +487,7 @@
   public :
 
-    /// \brief General constructor of the class (with lower bounds).
-    ///
-    /// General constructor of the class (with lower bounds).
+    /// \brief General constructor (with lower bounds).
+    ///
+    /// General constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -507,5 +506,6 @@
       _pred_edge(_graph), _thread(_graph), _forward(_graph),
       _state(_graph), _red_cost(_graph, _cost, _potential),
-      _flow_result(graph), _potential_result(graph),
+      _flow_result(0), _potential_result(0),
+      _local_flow(false), _local_potential(false),
       _node_ref(graph), _edge_ref(graph)
     {
@@ -539,7 +539,7 @@
     }
 
-    /// \brief General constructor of the class (without lower bounds).
-    ///
-    /// General constructor of the class (without lower bounds).
+    /// \brief General constructor (without lower bounds).
+    ///
+    /// General constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -556,5 +556,6 @@
       _pred_edge(_graph), _thread(_graph), _forward(_graph),
       _state(_graph), _red_cost(_graph, _cost, _potential),
-      _flow_result(graph), _potential_result(graph),
+      _flow_result(0), _potential_result(0),
+      _local_flow(false), _local_potential(false),
       _node_ref(graph), _edge_ref(graph)
     {
@@ -575,7 +576,7 @@
     }
 
-    /// \brief Simple constructor of the class (with lower bounds).
-    ///
-    /// Simple constructor of the class (with lower bounds).
+    /// \brief Simple constructor (with lower bounds).
+    ///
+    /// Simple constructor (with lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -599,5 +600,6 @@
       _pred_edge(_graph), _thread(_graph), _forward(_graph),
       _state(_graph), _red_cost(_graph, _cost, _potential),
-      _flow_result(graph), _potential_result(graph),
+      _flow_result(0), _potential_result(0),
+      _local_flow(false), _local_potential(false),
       _node_ref(graph), _edge_ref(graph)
     {
@@ -626,7 +628,7 @@
     }
 
-    /// \brief Simple constructor of the class (without lower bounds).
-    ///
-    /// Simple constructor of the class (without lower bounds).
+    /// \brief Simple constructor (without lower bounds).
+    ///
+    /// Simple constructor (without lower bounds).
     ///
     /// \param graph The directed graph the algorithm runs on.
@@ -648,5 +650,6 @@
       _pred_edge(_graph), _thread(_graph), _forward(_graph),
       _state(_graph), _red_cost(_graph, _cost, _potential),
-      _flow_result(graph), _potential_result(graph),
+      _flow_result(0), _potential_result(0),
+      _local_flow(false), _local_potential(false),
       _node_ref(graph), _edge_ref(graph)
     {
@@ -663,4 +666,44 @@
     }
 
+    /// Destructor.
+    ~NetworkSimplex() {
+      if (_local_flow) delete _flow_result;
+      if (_local_potential) delete _potential_result;
+    }
+
+    /// \brief Sets the flow map.
+    ///
+    /// Sets the flow map.
+    ///
+    /// \return \c (*this)
+    NetworkSimplex& flowMap(FlowMap &map) {
+      if (_local_flow) {
+        delete _flow_result;
+        _local_flow = false;
+      }
+      _flow_result = &map;
+      return *this;
+    }
+
+    /// \brief Sets the potential map.
+    ///
+    /// Sets the potential map.
+    ///
+    /// \return \c (*this)
+    NetworkSimplex& potentialMap(PotentialMap &map) {
+      if (_local_potential) {
+        delete _potential_result;
+        _local_potential = false;
+      }
+      _potential_result = &map;
+      return *this;
+    }
+
+    /// \name Execution control
+    /// The only way to execute the algorithm is to call the run()
+    /// function.
+
+    /// @{
+
     /// \brief Runs the algorithm.
     ///
@@ -704,4 +747,13 @@
     }
 
+    /// @}
+
+    /// \name Query Functions
+    /// The result of the algorithm can be obtained using these
+    /// functions.
+    /// \n run() must be called before using them.
+
+    /// @{
+
     /// \brief Returns a const reference to the edge map storing the
     /// found flow.
@@ -711,5 +763,5 @@
     /// \pre \ref run() must be called before using this function.
     const FlowMap& flowMap() const {
-      return _flow_result;
+      return *_flow_result;
     }
 
@@ -722,5 +774,23 @@
     /// \pre \ref run() must be called before using this function.
     const PotentialMap& potentialMap() const {
-      return _potential_result;
+      return *_potential_result;
+    }
+
+    /// \brief Returns the flow on the edge.
+    ///
+    /// Returns the flow on the edge.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Capacity flow(const typename Graph::Edge& edge) const {
+      return (*_flow_result)[edge];
+    }
+
+    /// \brief Returns the potential of the node.
+    ///
+    /// Returns the potential of the node.
+    ///
+    /// \pre \ref run() must be called before using this function.
+    Cost potential(const typename Graph::Node& node) const {
+      return (*_potential_result)[node];
     }
 
@@ -734,7 +804,9 @@
       Cost c = 0;
       for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e)
-        c += _flow_result[e] * _cost[_edge_ref[e]];
+        c += (*_flow_result)[e] * _cost[_edge_ref[e]];
       return c;
     }
+
+    /// @}
 
   private:
@@ -744,4 +816,14 @@
     bool init() {
       if (!_valid_supply) return false;
+
+      // Initializing result maps
+      if (!_flow_result) {
+        _flow_result = new FlowMap(_graph_ref);
+        _local_flow = true;
+      }
+      if (!_potential_result) {
+        _potential_result = new PotentialMap(_graph_ref);
+        _local_potential = true;
+      }
 
       // Initializing state and flow maps
@@ -1016,12 +1098,12 @@
       if (_lower) {
         for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e)
-          _flow_result[e] = (*_lower)[e] + _flow[_edge_ref[e]];
+          (*_flow_result)[e] = (*_lower)[e] + _flow[_edge_ref[e]];
       } else {
         for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e)
-          _flow_result[e] = _flow[_edge_ref[e]];
+          (*_flow_result)[e] = _flow[_edge_ref[e]];
       }
       // Copying potential values to _potential_result
       for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n)
-        _potential_result[n] = _potential[_node_ref[n]];
+        (*_potential_result)[n] = _potential[_node_ref[n]];
 
       return true;
