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);
     }
 
